Ask Your Question

Revision history [back]

Try

ZZ(y).inverse_mod(p)

TryIf you define

y = np.int(tmp)

then do

ZZ(y).inverse_mod(p)

Better: directly define

y = ZZ(tmp)

and then do

y.inverse_mod(p)

If (Edited to answer the follow-up question posted as a comment.)

Your code uses numpy, which uses Python's int type.

Sage has its own type of integers, in addition to Python integers.

Sage methods such as inverse_mod are implemented for Sage integers, not Python integers.

You can convert a Python integer k to a Sage integer by calling Integer(k) or ZZ(k).

So, if you definedefine, as in your code,

y = np.int(tmp)

then dochange the last line to

ZZ(y).inverse_mod(p)

Better: directly define

y = ZZ(tmp)

and then do

y.inverse_mod(p)

Now, you might want to avoid numpy altogether and work with native Sage matrices and vectors.

You can initialize a matrix or vector by specifying the base ring (eg ZZ, QQ, etc), the dimension, and the entries. If you don't provide entries, you get a zero matrix or vector. Matrices and vectors are mutable by default.

Rows, columns, and vector components are indexed from 0 rather than 1.

sage: a = matrix(ZZ, 3)
sage: a
[0 0 0]
[0 0 0]
[0 0 0]
sage: a[0, 0] = 4
sage: a
[4 0 0]
[0 0 0]
[0 0 0]

sage: v = vector(ZZ, 3)
sage: v
(0, 0, 0)
sage: v[1] = 2
sage: v
(0, 2, 0)
sage: a * v
(0, 0, 0)

You can find lots more information and examples in the documentation and tutorials, eg

http://doc.sagemath.org/html/en/tutorial/tour_linalg.html

See also the quick reference manual

https://wiki.sagemath.org/quickref?action=AttachFile&do=get&target=quickref-linalg.pdf

or query your favourite web search engine for [sagemath linear algebra].