giacpy and changing a matrix element

Répondre
jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

giacpy and changing a matrix element

Message par jocaps » mer. août 09, 2017 2:05 pm

Hi

I was wondering how you can change a matrix element in giacpy. For instance, consider this code

Code : Tout sélectionner

from giacpy import newMat

m = newMat(4,8)
m[1,1]=2
the last line of the code will give me an error:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
m[1,1]=2
TypeError: 'giacpy.Pygen' object does not support item assignment

frederic han
Messages : 1137
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: giacpy and changing a matrix element

Message par frederic han » jeu. août 10, 2017 6:15 pm

Indeed this is not convienient in giacpy.
So depending on the situation you may try to avoid it by working with python lists of lists and then send this to giac.

To really do it there is subsop. But I don't know why the second syntax of subsop (without using =) seems broken in giac.
cf: http://xcas.e.ujf-grenoble.fr/XCAS/view ... f=3&t=1837

so we must use the first one (with the = symbol) so with must use strings.

Code : Tout sélectionner

>>> import giacpy
>>> from giacpy import giac
>>> x=giac('x')
>>> p=1+x**2
>>> m=giacpy.matrix(5,5)
>>> m
matrix[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
>>> m=m.subsop("[0,1]=%s"%p) 
while

Code : Tout sélectionner

>>> m=m.subsop([0,1],p)
would have worked if subsop is fixed in giac.

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

Re: giacpy and changing a matrix element

Message par jocaps » jeu. août 17, 2017 8:58 am

frederic han a écrit :Indeed this is not convienient in giacpy.So depending on the situation you may try to avoid it by working with python lists of lists and then send this to giac.
Thank you Frederic. The problem with python list of list is that you want to do matrix operations in between and have to transform to matrix every often. But you are right, I think this is the best option at the moment. In the end, what I did was very similar. I used a flat list in python to modify as much as I want and then use list2mat with correct size for columns to convert back. But for continuous change of matrix entries, I should indeed go with the subsop option you suggested. It seems that the option without "=" will soon be implemented/corrected by Bernard (last post you cited). But it also seems that giacpy is not being constantly updated/compiled for windows (last version was 0.4.4 and upated version for linux is 0.5.3). Is there a guideline on how to compile the python library for windows (I could update my version by downloading the source from git and compile for windows)? Did you build it also by simply executing (I am imagining some dependency for windows that is needed to be obtained/linked?)

Code : Tout sélectionner

python setup.py build_ext

Thanks for the advise again.

frederic han
Messages : 1137
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: giacpy and changing a matrix element

Message par frederic han » jeu. août 17, 2017 9:32 pm

NB: you can also use the giac command to create a giac matrix from a list of list like this where A is a python list of list and B is a giac matrix:

Code : Tout sélectionner

>>> from giacpy import giac
>>> x,y=giac('x,y')
>>> A=[[1,x+1,2],[y+x,1/x,y],[(x+y)**2,3,4]]
>>> B=giac(A)
>>> B.det()
(x**4*y+2*x**3*y**2+x**3*y-4*x**3+x**2*y**3+2*x**2*y**2-4*x**2*y+x*y**3-5*x*y-2*y**2+4)/x
jocaps a écrit :
frederic han a écrit :Indeed this is not convienient in giacpy. So depending on the situation you may try to avoid it by working with python lists of lists and then send this to giac.
Did you build it also by simply executing (I am imagining some dependency for windows that is needed to be obtained/linked?)

Code : Tout sélectionner

python setup.py build_ext
Unfortunately I have never suceeded to do this because I would need to build first the giac library on windows and I got always linkage problems with python. So up to now I am cross buildings the giac library and giacpy from linux with mingw.
NB: the giacpy version only refers to the cython code (giacpy) but it can be built with any giac library. We don't need to change the giacpy code to benefit of the new subsop syntax.

Up to now I have patched giac for subsop and built a giac.dll (with pari) and giacpy for python3.5 in 64 bits. But I have not yet packaged it.
What version of python do you need (Ex: 2.7, 3.5..) and for python 32bits or 64bits?

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

Re: giacpy and changing a matrix element

Message par jocaps » ven. août 18, 2017 12:52 pm

frederic han a écrit : Unfortunately I have never suceeded to do this because I would need to build first the giac library on windows and I got always linkage problems with python. So up to now I am cross buildings the giac library and giacpy from linux with mingw.
NB: the giacpy version only refers to the cython code (giacpy) but it can be built with any giac library. We don't need to change the giacpy code to benefit of the new subsop syntax.
I see. So, you think, if I just change the dll to an updatd giac.dll that has the new subsop syntax it will work? In this case I can just build giac.dll with mingw without relinking with the giacpy pyd.
frederic han a écrit : Up to now I have patched giac for subsop and built a giac.dll (with pari) and giacpy for python3.5 in 64 bits. But I have not yet packaged it.
What version of python do you need (Ex: 2.7, 3.5..) and for python 32bits or 64bits?
Ah, yes it would be nice to have this. Esp. that you write that the package you have is with pari (which would be magnificent for me, since I am separately using a pari library for python in windows). Pari for python is not well-organized as giacpy is and this will benefit me a lot (it is only compiled with cygwin so you need to run python under cygwin). The giacpy (for windows) that I have so far downloaded were built (I think) without pari.

I think, for me, the safest is to get the library created for Python 2.7 and 32bits. You could maybe just put up a link for me to get it. But in the long run, maybe it is a lot of work for you to always manage the different packages. Do you not plan to automatize this process to make this easier in the future? Thanks in any case.

I am now solely dependent on giac (using giacpy) since a few months now and it has been wonderful.

frederic han
Messages : 1137
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: giacpy and changing a matrix element

Message par frederic han » ven. août 18, 2017 8:30 pm

Here is a python27 win32 version of giacpy with pari gsl gmp mpfr and ntl.

http://webusers.imj-prg.fr/~frederic.ha ... 123-57.zip
(extract it in your site-package of python2.7 win32)

pari keywords are not exported in giacpy but you can have it easily with the giac function like this:

Code : Tout sélectionner

>> from giacpy import giac
// Giac share root-directory:/usr/share/giac/
// Giac share root-directory:/usr/share/giac/
Help file /usr/share/giac/doc/fr/aide_cas not found
Added 0 synonyms

>>> giac('pari()')  # load pari keywords in giac
"All PARI functions are now defined with the pari_ prefix.
PARI functions are also defined without prefix except:
% abs acos acosh apply arg asin asinh atan atanh binomial bitand bitor bitxor break ceil charpoly concat conj content cos cosh default divisors erfc eval exp factor factorial floor frac gcd global hilbert imag isprime kill lcm length local matrix max min next nextprime norm print printf real round select shift sign simplify sin sinh solve sqrt subst sum tan tanh taylor trace truncate type until valuation vector version write 
When working with p-adic numbers use them in a pari() call
Type ?pari for short help
Inside xcas, try Help->Manuals->PARI for HTML help"

>>> pari_factor=giac('pari_factor') # pari_factor is not exported in giacpy but we can export it like this

>>> pari_factor(2**128+1)
matrix[[59649589127497217,1],[5704689200685129054721,1]]
I hope it will work for you. I also have a python3.5 win64 if you want.

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

Re: giacpy and changing a matrix element

Message par jocaps » sam. août 19, 2017 8:20 pm

frederic han a écrit :Here is a python27 win32 version of giacpy with pari gsl gmp mpfr and ntl.
...
...
I hope it will work for you. I also have a python3.5 win64 if you want.
Thank you Frederic. It does work charmingly. Thanks a lot. subsop works as well. Pari works to the extent that I checked (I tried to initialize a complicated elliptic curve with ellinit and that worked). Thanks to you, now i would not need to import pari from another library.

Jose

Edit: I just discovered a problem with this version that I did not have with version 0.4.4. A simple example that will illustrate this problem is the following code that tries to symbolically compute the nullspace of a complicated matrix:

Code : Tout sélectionner

from giacpy import matrix, ker
m=matrix("[(-2*l1+2)/2,(2*l1+2)/2,0,0,(l1*a2+a2)/2,(l1*a2-a2)/2,(l1*d2-d2)/2,(-l1*d2-d2)/2,0,0,0,0,0,0,0,0],[0,0,(-2*l1+2)/2,(2*l1+2)/2,(-l1*d2+d2)/2,(l1*d2+d2)/2,(-l1*a2-a2)/2,(-l1*a2+a2)/2,(-2*l1+2)/2,(2*l1+2)/2,0,0,(l1*a2+a2)/2,(l1*a2-a2)/2,(l1*d2-d2)/2,(-l1*d2-d2)/2],[0,0,0,0,0,0,0,0,0,0,(-2*l1+2)/2,(2*l1+2)/2,(-l1*d2+d2)/2,(l1*d2+d2)/2,(-l1*a2-a2)/2,(-l1*a2+a2)/2],[0,0,0,0,0,0,(l1+1)/2,(l1-1)/2,0,0,0,0,0,0,0,0],[0,0,0,0,(l1+1)/2,(l1-1)/2,0,0,0,0,0,0,0,0,(l1+1)/2,(l1-1)/2],[0,0,0,0,0,0,0,0,0,0,0,0,(l1+1)/2,(l1-1)/2,0,0],[0,0,(2*l1+2)/2,(-2*l1+2)/2,(l1*d2+d2)/2,(-l1*d2+d2)/2,(l1*a2-a2)/2,(l1*a2+a2)/2,0,0,0,0,0,0,0,0],[(-2*l1-2)/2,(2*l1-2)/2,0,0,(l1*a2-a2)/2,(l1*a2+a2)/2,(l1*d2+d2)/2,(-l1*d2+d2)/2,0,0,(2*l1+2)/2,(-2*l1+2)/2,(l1*d2+d2)/2,(-l1*d2+d2)/2,(l1*a2-a2)/2,(l1*a2+a2)/2],[0,0,0,0,0,0,0,0,(-2*l1-2)/2,(2*l1-2)/2,0,0,(l1*a2-a2)/2,(l1*a2+a2)/2,(l1*d2+d2)/2,(-l1*d2+d2)/2],[0,0,0,0,(-l1+1)/2,(-l1-1)/2,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,(l1-1)/2,(l1+1)/2,0,0,0,0,(-l1+1)/2,(-l1-1)/2,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,(l1-1)/2,(l1+1)/2]")

ker(m)
with this version I get the following error:

Code : Tout sélectionner

"sym2poly/r2sym(const gen & e,const index_m & i,const vecteur & l) Error: Bad Argument Value"
This could be more of a giac problem than a giacpy problem. Could you reproduce this error as well?
With giacpy version 0.4.4 I get the nullspace (I haven't verify its correctness but I would assume it is correct).

frederic han
Messages : 1137
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: giacpy and changing a matrix element

Message par frederic han » sam. août 19, 2017 10:18 pm

Edit: I just discovered a problem with this version that I did not have with version 0.4.4. A simple example that will illustrate this problem is the following code that tries to symbolically compute the nullspace of a complicated matrix:

Code : Tout sélectionner

from giacpy import matrix, ker
m=matrix("[(-2*l1+2)/2,(2*l1+2)/2,0,0,(l1*a2+a2)/2,(l1*a2-a2)/2,(l1*d2-d2)/2,(-l1*d2-d2)/2,0,0,0,0,0,0,0,0],[0,0,(-2*l1+2)/2,(2*l1+2)/2,(-l1*d2+d2)/2,(l1*d2+d2)/2,(-l1*a2-a2)/2,(-l1*a2+a2)/2,(-2*l1+2)/2,(2*l1+2)/2,0,0,(l1*a2+a2)/2,(l1*a2-a2)/2,(l1*d2-d2)/2,(-l1*d2-d2)/2],[0,0,0,0,0,0,0,0,0,0,(-2*l1+2)/2,(2*l1+2)/2,(-l1*d2+d2)/2,(l1*d2+d2)/2,(-l1*a2-a2)/2,(-l1*a2+a2)/2],[0,0,0,0,0,0,(l1+1)/2,(l1-1)/2,0,0,0,0,0,0,0,0],[0,0,0,0,(l1+1)/2,(l1-1)/2,0,0,0,0,0,0,0,0,(l1+1)/2,(l1-1)/2],[0,0,0,0,0,0,0,0,0,0,0,0,(l1+1)/2,(l1-1)/2,0,0],[0,0,(2*l1+2)/2,(-2*l1+2)/2,(l1*d2+d2)/2,(-l1*d2+d2)/2,(l1*a2-a2)/2,(l1*a2+a2)/2,0,0,0,0,0,0,0,0],[(-2*l1-2)/2,(2*l1-2)/2,0,0,(l1*a2-a2)/2,(l1*a2+a2)/2,(l1*d2+d2)/2,(-l1*d2+d2)/2,0,0,(2*l1+2)/2,(-2*l1+2)/2,(l1*d2+d2)/2,(-l1*d2+d2)/2,(l1*a2-a2)/2,(l1*a2+a2)/2],[0,0,0,0,0,0,0,0,(-2*l1-2)/2,(2*l1-2)/2,0,0,(l1*a2-a2)/2,(l1*a2+a2)/2,(l1*d2+d2)/2,(-l1*d2+d2)/2],[0,0,0,0,(-l1+1)/2,(-l1-1)/2,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,(l1-1)/2,(l1+1)/2,0,0,0,0,(-l1+1)/2,(-l1-1)/2,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,(l1-1)/2,(l1+1)/2]")

ker(m)
Indeed with xcas on linux it crashes. I will report this in another topic.

frederic han
Messages : 1137
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: giacpy and changing a matrix element

Message par frederic han » dim. août 20, 2017 3:41 pm

OK I have rebuilt the giac.dll (1.2.3-57) with the subsop and the row reduction patch reported
http://xcas.e.ujf-grenoble.fr/XCAS/view ... f=3&t=1846
, and rebuilt giacpy. You can find it here:
http://webusers.imj-prg.fr/~frederic.ha ... 3-57p1.zip

Thank you for your feed back I was worrying if giacpy was usefull. I may built 64bit versions soon.
Best frederic

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

Re: giacpy and changing a matrix element

Message par jocaps » lun. août 21, 2017 11:14 am

frederic han a écrit :OK I have rebuilt the giac.dll (1.2.3-57) with the subsop and the row reduction patch reported
http://xcas.e.ujf-grenoble.fr/XCAS/view ... f=3&t=1846
, and rebuilt giacpy. You can find it here:
http://webusers.imj-prg.fr/~frederic.ha ... 3-57p1.zip

Thank you for your feed back I was worrying if giacpy was usefull. I may built 64bit versions soon.
Best frederic
Thank you Frederic. I confirm that it works now.

Yes, I prefer this than sage both in windows (especially in windows!) and linux systems. It is clean, clear, minimal (in size), buildable (for me) and I can fully manage what python library I want to have. And (for windows) I do not need any virtual machine. So for me, giacpy is more than useful .. it is necessary and I am very thankful for it. I'll communicate through forum if there is any other issue.

Jose

frederic han
Messages : 1137
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: giacpy and changing a matrix element

Message par frederic han » ven. août 25, 2017 11:48 am

Hello I have improved my building tool chain and was able to obtain wheels for the firstime!
I have uploaded giacpy to Pypi for python 2.7, 3.5, 3.6 for win32 and amd64 so

Code : Tout sélectionner

python -mpip install giacpy
should work

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

Re: giacpy and changing a matrix element

Message par jocaps » ven. août 25, 2017 1:19 pm

I confirm installation with pip under windows. It is great that it is also compiled with pari and so the pari library is included in the installation. But I also confirm that the last patch with "ker" fix is not there.

Jose

frederic han
Messages : 1137
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: giacpy and changing a matrix element

Message par frederic han » ven. août 25, 2017 5:09 pm

thank you, indeed I forgot this.
To clarify things I have uploaded this new binary to pypi with giacpy version bumped to 0.5.4.

frederic han
Messages : 1137
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

Re: giacpy and changing a matrix element

Message par frederic han » jeu. sept. 14, 2017 12:12 pm

In giacpy 0.6.0rc1 (cf:
http://webusers.imj-prg.fr/~frederic.ha ... -win32.whl

I have added a __setitem__ method so it should be easier. I also enhanced the docstring of giacpy.giac like this:

it should be faster than subsop but it is an "in place" affectation while subsop recopy all the matrix.

also for large matrix:

A=giacpy.ranm(5000,5000)
A[1000][1000]=1 seems faster that A[1000,1000]=1 (cf. the doc of giacpy.Pygen.__setitem__)

Code : Tout sélectionner

   Linear Algebra:
   ---------------

   * In Giac/Xcas vectors are just lists and matrices are lists of list.

    >>> x,y=giac('x,y')
    >>> A=giac([[1,2],[3,4]])  # we create a giac matrix from it lines
    >>> v=giac([x,y]); v   # a giac vector
    [x,y]
    >>> A*v # matrix product with a vector outputs a vector
    [x+2*y,3*x+4*y]
    >>> v*v  # dot product
    x*x+y*y

    Remark that w=giac([[x],[y]]) is a matrix of 1 column and 2 rows. It is not a vector\
    so w*w doesn't make sense.
    >>> w=giac([[x],[y]])
    >>> w.transpose()*w   # this matrix product makes sense and output a 1x1 matrix.
    matrix[[x*x+y*y]]

   * In Python affectation doesn't create a new matrix. (cf. pointers) see also \
 the doc of  'giacpy.Pygen.__setitem__'

    >>> B1=A;
    >>> B1[0,0]=43; B1 # in place affectation changes both B1 and A
    [[43,2],[3,4]]
    >>> A
    [[43,2],[3,4]]
    >>> A[0][0]=A[0][0]+1; A  # similar as A[0,0]=A[0,0]+1
    [[44,2],[3,4]]
    >>> A.pcar(x)  # compute the characteristic polynomial of A
    x**2-48*x+170
    >>> B2=A.copy() # use copy to create another object
    >>> B2[0,0]=55; B2  # here A is not modified
    [[55,2],[3,4]]
    >>> A
    [[44,2],[3,4]]



   * Sparse Matrices are avaible via the table function.
    >>> import giacpy
    >>> A=giacpy.table(()); A  # create an empty giac table
    table(
    )
    >>> A[2,3]=33; A[0,2]='2/7' # set non zero entries of the sparse matrix
    >>> A*A  # basic matrix operation are supported with sparse matrices
    table(
    (0,3) = 66/7
    )
    >>> D=giacpy.diag([22,3,'1/7']); D  # some diagonal matrix
    [[22,0,0],[0,3,0],[0,0,1/7]]
    >>> giacpy.table(D)    # to create a sparse matrix from an ordinary one
    table(
    (0,0) = 22,
    (1,1) = 3,
    (2,2) = 1/7
    )


     But many matrix functions apply only with ordinary matrices so need conversions

    >>> B1=A.matrix(); B1 # convert the sparse matrix to a matrix, but the size is minimal
    [[0,0,2/7,0],[0,0,0,0],[0,0,0,33]]
    >>> B2=B1.redim(4,4); B2.pmin(x)  # so we may need to resize B1
    x**3

Répondre