Ask Your Question

use type signature in functions for matrix and vector

asked 2020-12-01 01:34:28 +0100

mocha gravatar image


I'm trying to create a function that takes a vector and a matrix. I want to explicitly used typed variables but I'm not sure what to type for the types matrix and vector.

Example with : fun(i:int): return 0

So what is the equivalent for this with func(M, V)? I tried func(M: matrix, V :vector) and func(M: sage.matrix, V :sage.vector) but both gave : "name 'matrix' is not defined"

Thanks in advance

edit retag flag offensive close merge delete


Welcome to Ask Sage!

Thank you for your question!

slelievre gravatar imageslelievre ( 2020-12-01 05:22:29 +0100 )edit

It seems you opened two accounts and asked the question twice, here and at

Sorry that our moderation takes a little time.

slelievre gravatar imageslelievre ( 2020-12-01 05:25:45 +0100 )edit

Hello, @mocha! I am not very versed on type hinting in Python. please, let me know if this solves your problem, so i can elaborate it and post it as an answer.

You can exploit the type() function for this purpose. For example, define

v = vector([1,2,3])

and then write type(v) in order to know the specific data type. In this particular case, it is sage.modules.vector_integer_dense.Vector_integer_dense. Of course, this is way too long to be practical, so maybe you could simply make something like myVector = sage.modules.vector_integer_dense.Vector_integer_dense and then use myVector for short.

Do be careful with using myVector arbitrarily. For example, vector([1., 2., 3.]) is of type sage.modules.free_module_element.FreeModuleElement_generic_dense.

I hope this helps!

dsejas gravatar imagedsejas ( 2020-12-01 05:57:39 +0100 )edit

Sorry for the duplicate, I got a message that my first post was flagged as spam, so I thought It didn't pass trough. I removed the other post.

Thanks a lot for your reply. This is actually what I have done but I was looking for a shorter syntax. For example for the type "sage.rings.integer.Integer" it's enough to use "int" so I was wondering if there is a similarly short way to achieve this for matrix and vector types.

mocha gravatar imagemocha ( 2020-12-01 20:17:50 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-02 05:28:31 +0100

dsejas gravatar image

Hello, @mocha! You should be very careful about Python data types and Sage data types. In particular, int is a Python data type, but sage.rings.integer.Integer is a Sage data type. Although they behave quite similarly, there are differences. For example, try int(5).is_prime(), which produces an error because uses int instead of Integer.

Anyway, you could create a module where you define, for example, svector = sage.modules.vector_integer_dense.Vector_integer_dense. I suggest using the Union() function from the typing module to create a more complete svector type that also includes sage.modules.free_module_element.FreeModuleElement_generic_dense and other Sage vector types. Finally, just make something like from my_data_types import svector.

edit flag offensive delete link more


I turned your comment into an answer, hope that's ok.

slelievre gravatar imageslelievre ( 2020-12-02 15:30:55 +0100 )edit

Thank you very much, @slelievre! I wasn't completely sure if this was good enough as an answer.

dsejas gravatar imagedsejas ( 2020-12-03 05:49:18 +0100 )edit

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


Asked: 2020-12-01 01:34:28 +0100

Seen: 411 times

Last updated: Dec 01 '20