# use type signature in functions for matrix and vector

Hi,

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"

edit retag close merge delete

Welcome to Ask Sage!

Thank you for your question!

( 2020-12-01 05:22:29 +0200 )edit

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

Sorry that our moderation takes a little time.

( 2020-12-01 05:25:45 +0200 )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!

( 2020-12-01 05:57:39 +0200 )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.

( 2020-12-01 20:17:50 +0200 )edit

Sort by » oldest newest most voted

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 my_data_types.py 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.

more

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

( 2020-12-02 15:30:55 +0200 )edit

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

( 2020-12-03 05:49:18 +0200 )edit