Creating a callable matrix with functions.

166 views
Skip to first unread message

Jaasiel Ornelas

unread,
Nov 22, 2011, 5:50:07 PM11/22/11
to sage-support
Hi!

I'm trying to get a matrix over SR to be callable, much like vectors
are. My code consists of the following:

"
(theta,beta,gamma)=var('theta,beta,gamma')
R_theta=matrix(SR,[[cos(theta),sin(theta),0],[-sin(theta),cos(theta),
0],[0,0,1]])
R_beta=matrix(SR,3,3,[cos(beta),0,-sin(beta),0,1,0,sin(beta),
0,cos(beta)])
"

Defining the matrices with a place for the variables yields an error
with mutiplication:

"
R_theta(theta)=matrix(SR,[[cos(theta),sin(theta),0],[-
sin(theta),cos(theta),0],[0,0,1]])
R_beta(beta)=matrix(SR,3,3,[cos(beta),0,-sin(beta),0,1,0,sin(beta),
0,cos(beta)])

R_theta*T_beta

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_89.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding:
utf-8 -*-\\n" +
_support_.preparse_worksheet_cell(base64.b64decode("Ul90aGV0YSpSX2JldGE="),globals())
+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>

File "/private/var/folders/bM/bMOEaVgXE3CHfi1Ua6Udb++++TI/-Tmp-/
tmpNfeDiM/___code___.py", line 2, in <module>
exec compile(u'R_theta*R_beta
File "", line 1, in <module>

File "element.pyx", line 1459, in
sage.structure.element.RingElement.__mul__ (sage/structure/element.c:
12096)
File "coerce.pyx", line 714, in
sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/
coerce.c:6436)
File "element.pyx", line 1454, in
sage.structure.element.RingElement.__mul__ (sage/structure/element.c:
12005)
File "expression.pyx", line 2216, in
sage.symbolic.expression.Expression._mul_ (sage/symbolic/
expression.cpp:11504)
ArithmeticError: Number_T::hash() python function (__hash__) raised
exception
"

In either case, I can't get sage to substitue the variable in the
matrix. (I can define the variables before defining the matrix, but
that is redundant) Any ideas?

Thanks a lot!

Thanks a lot!

achrzesz

unread,
Nov 23, 2011, 3:20:06 AM11/23/11
to sage-support
Maybe:

sage:def R_theta(theta):
return matrix(SR,[[cos(theta),sin(theta),0],[-sin(theta),cos(theta),
0],[0,0,1]])
sage: R_theta(x)
[ cos(x) sin(x) 0]
[-sin(x) cos(x) 0]
[ 0 0 1]
sage: R_theta(1)
[ cos(1) sin(1) 0]
[-sin(1) cos(1) 0]
[ 0 0 1]
sage: R_theta
<function R_theta at 0x5e83848>

Andrzej Chrzeszczyk

Tom Boothby

unread,
Nov 23, 2011, 3:32:53 AM11/23/11
to sage-s...@googlegroups.com
This is a little on the terse side, but it works quite generally.

def MatrixCallable(M):
N = Matrix(SR,M)
def m(*a,**k):
return Matrix([[e(*a,**k) for e in row] for row in N])
return m

sage: x = var('x')
sage: M = [[sin(x),cos(x),0],[cos(x),sin(x),x^3]]
sage: N = MatrixCallable(M)
sage: N(x=pi/4)
[1/2*sqrt(3) 1/2 0]
[ 1/2 1/2*sqrt(3) 1/27*pi^3]

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>

Jason Grout

unread,
Nov 23, 2011, 7:12:40 AM11/23/11
to sage-s...@googlegroups.com
On 11/23/11 1:32 AM, Tom Boothby wrote:
> This is a little on the terse side, but it works quite generally.


And it isn't quite the same as Callable symbolic expressions. I.e., if
you have two variables, you can't specify the order when you create the
matrix.

On the other hand, doing something like


sage: var('x,y')
(x, y)
sage:
M=matrix(CallableSymbolicExpressionRing((x,y)),[[x+2*y,sin(x),cos(y)],[x,y,x^2+y]])
sage: M(1,2)
[ 5 sin(1) cos(2)]
[ 1 2 3]

Really behaves like a callable expression. It prints sort of ugly, though:

sage: M
[(x, y) |--> x + 2*y (x, y) |--> sin(x) (x, y) |--> cos(y)]
[ (x, y) |--> x (x, y) |--> y (x, y) |--> x^2 + y]


That could be improved, e.g., see the printing for vectors over callable
symbolic rings, which print the arguments outside of the vector:

sage: M[0]
(x, y) |--> (x + 2*y, sin(x), cos(y))


Note that the syntax f(x,y)=... is preparsed as:

sage: preparse('f(x,y)=[[x^2,y],[x,y]]')
'__tmp__=var("x,y"); f =
symbolic_expression([[x**Integer(2),y],[x,y]]).function(x,y)'

so really all that is needed is changing the symbolic_expression
function to create a symbolic matrix if a list of lists (or tuples) is
given, and then adding a .function() method to a symbolic matrix to give
a matrix with base ring CallableSymbolicExpressionRing (as above).
Bonus points if you make those matrices print out with the arguments
first, instead of having the arguments inside of each element. I've
done this for vectors [1], so that could be a pattern for what to do here.

Thanks,

Jason

[1] See the symbolic_expression code, which has this case:

elif isinstance(x, (tuple,list)):
return vector(SR,x)

jrodr...@gmail.com

unread,
Nov 23, 2011, 11:19:48 AM11/23/11
to sage-s...@googlegroups.com
I'm a physics guy, so programing is not at the top of the to-do list for me (yet), but I might take it on, since I've been meaning to learn python as a primer for programming in general. Also, I use sage over all the other software they have at school, and sometimes bugs in sage keep me too busy. Getting to know it's innards might help those situations out.

Thanks!

Jason Grout

unread,
Nov 23, 2011, 11:38:20 AM11/23/11
to sage-s...@googlegroups.com
On 11/23/11 9:19 AM, jrodr...@gmail.com wrote:
> I'm a physics guy, so programing is not at the top of the to-do list for me (yet), but I might take it on, since I've been meaning to learn python as a primer for programming in general. Also, I use sage over all the other software they have at school, and sometimes bugs in sage keep me too busy. Getting to know it's innards might help those situations out.
>

I think this would be a good project for a person wanting to learn more
about how to do Sage development. The modifications to
symbolic_expression should be easy (check for a nested list/tuple
structure). Then you would need to make a new function() method, in
$SAGE_ROOT/devel/sage/sage/matrix/matrix2.pyx probably, that would
create a corresponding matrix over the CallableSymbolicRing. You can
see an example for vectors in
$SAGE_ROOT/devel/sage/sage/modules/free_module.pyx (look for "def
function" in that file).

Then if you are ambitious, create a new matrix subclass for callable
symbolic expressions that mirrors what
$SAGE_ROOT/devel/sage/sage/modules/vector_callable_symbolic_dense.py
does for vectors (i.e., changes the printing).

Thanks,

Jason


kcrisman

unread,
Nov 23, 2011, 12:52:48 PM11/23/11
to sage-support
This is now http://trac.sagemath.org/sage_trac/ticket/12075.

On Nov 23, 11:38 am, Jason Grout <jason-s...@creativetrax.com> wrote:

Reply all
Reply to author
Forward
0 new messages