Page 1 sur 1
newMat and shallow copy
Publié : ven. déc. 01, 2017 8:39 am
par jocaps
Hi,
I am not sure if I should report this as a bug or if it is my misunderstanding. Throughout I am using giacpy 0.6.1
Consider the following code:
The result is a (duplicate of) 2 on all of the entries of the second column of the matrix i.e. we get the output [[0,2],[0,2]]. Of course what I/we want is [[0,2],[0,0]]
It seems that newMat does a shallow copy of [0,0]. This is similar to the following simple python command (which has the same problem because python does a shallow copy of the vectors:
declaring the matrix directly instead of using newMat will not have this problem (vectors are nor referenced to each other!) and have the desired effect:
Re: newMat and shallow copy
Publié : ven. déc. 01, 2017 9:52 am
par frederic han
indeed I have used in giacpy the same behaviour as python lists to be more efficient, while in giac/xcas the default affectation is := and recopy everything.
The problem is the same in giac/xcas with newMat so it should be changed there.
Code : Tout sélectionner
2>> A:=newMat(2,2)
[[0,0],[0,0]]
// Time 0
3>> A
[[0,0],[0,0]]
// Time 0
4>> A[1,1]=<2
[[0,2],[0,2]]
but as it is,
you have to find any trick to force different vectors. the following 2 seems to work but the first one looks faster:
Code : Tout sélectionner
from giacpy import matrix,NewList
In [84]: time n=1000;A=matrix(n,n,newList(n**2))
CPU times: user 77.3 ms, sys: 6.32 ms, total: 83.6 ms
Wall time: 75.9 ms
// Success
In [85]: time n=1000;A=matrix(n,n,'(u,v)->0')
Evaluation time: 1.15
CPU times: user 1.14 s, sys: 9.56 ms, total: 1.15 s
Wall time: 1.13 s
Re: newMat and shallow copy
Publié : ven. déc. 01, 2017 10:13 am
par jocaps
I don't know what the source code is in giac/giacpy that makes newMat. For python, simply writing
should be fast. I think loops are very fast in both python and C/C++ (unless there is very few things to do in the loop for which you need to unroll).
For the moment I use idn (identitty matrix) which does not suffer from this behaviour. Since I have a square matrix and I know I am going to change all the entries of the martrix I am not worried of using it. How does the identity matrix creating differ in giacpy? is it as fast as newMat (if so, maybe you can use a similar code for the implementation of newMat).
Re: newMat and shallow copy
Publié : ven. déc. 01, 2017 10:38 am
par frederic han
jocaps a écrit :I don't know what the source code is in giac/giacpy that makes newMat. For python, simply writing
should be fast. I think loops are very fast in both python and C/C++ (unless there is very few things to do in the loop for which you need to unroll).
For the moment I use idn (identitty matrix) which does not suffer from this behaviour. Since I have a square matrix and I know I am going to change all the entries of the martrix I am not worried of using it. How does the identity matrix creating differ in giacpy? is it as fast as newMat (if so, maybe you can use a similar code for the implementation of newMat).
Bernard will change newMat in the next giac version, so I will update the giac.dll (cf
http://xcas.e.ujf-grenoble.fr/XCAS/view ... f=4&t=1955)
So up to now the following 2 are similar and will also solve your problem.
Code : Tout sélectionner
from giacpy import idn,matrix,newList
n=1000
A=0*idn(n)
B=matrix(n,n,newList(n**2))