First time here? Check out the FAQ!

Ask Your Question
2

AttributeError: 'int' object has no attribute 'inverse_mod'

asked 8 years ago

fagui gravatar image

updated 8 years ago

slelievre gravatar image

Hi i'm a beginner with Sage, and i have some trouble with types. It looks like the cause of my pain is trying to use numpy together with Sagemath, is it possible to use Sagemath without numpy?

Here is a small example of an error that i encounter, when trying to do simple modular calculations in GF(7).

I use the following code:

import numpy as np

a=[2,4,6,1,3,5]
b=[1,1,1,1,1,1]
p=7
n=len(a)
L=1
c=np.zeros(n,'int')
Li_ai=np.zeros(n)
for i in range(n):
    L=(x-a[i])*L
for i in range(n):
    Li=L/(x-a[i])
    t=(Li(x=a[i])%p)
    Li_ai[i]=t
Li_ai=Li_ai.astype('int')
tmp=Li_ai.prod()%p
Li_ai=vector(Li_ai)
for i in range(n):
    c[i]=tmp * (Li_ai[i]) * b[i].inverse_mod(p) %p
c=vector(c)
c
y=np.int(tmp)
y.inverse_mod(p)

and I get the following error:

Traceback (most recent call last)
...
AttributeError: 'int' object has no attribute 'inverse_mod'
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 8 years ago

slelievre gravatar image

updated 8 years ago

(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 define, as in your code,

y = np.int(tmp)

then change 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].

Preview: (hide)
link

Comments

thank you. how do i create an 0 matrix or vector of integers in SageMath ? is this object mutable ? (for example make an assignment M[1,2]=3)

thanks

fagui gravatar imagefagui ( 8 years ago )

I edited my answer to answer this follow-up question too.

slelievre gravatar imageslelievre ( 8 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 8 years ago

Seen: 2,894 times

Last updated: Jun 05 '16