sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.41k stars 474 forks source link

preparse vector-valued functions and derivatives #8866

Closed jasongrout closed 14 years ago

jasongrout commented 14 years ago

Here is a rough patch which enables the following:

sage: T(r,theta)=[r*cos(theta),r*sin(theta)]
sage: T
((r, theta) |--> r*cos(theta), (r, theta) |--> r*sin(theta))
sage: T.diff() # Jacobian matrix
[   (r, theta) |--> cos(theta) (r, theta) |--> -r*sin(theta)]
[   (r, theta) |--> sin(theta)  (r, theta) |--> r*cos(theta)]
sage: diff(T) # Jacobian matrix
[   (r, theta) |--> cos(theta) (r, theta) |--> -r*sin(theta)]
[   (r, theta) |--> sin(theta)  (r, theta) |--> r*cos(theta)]
sage: T.diff().det() # Jacobian 
(r, theta) |--> r*sin(theta)^2 + r*cos(theta)^2

sage: f(x,y)=x^2+y
sage: f.diff() # gradient
((x, y) |--> 2*x, (x, y) |--> 1)
sage: f.diff().diff() # Hessian matrix
[(x, y) |--> 2 (x, y) |--> 0]
[(x, y) |--> 0 (x, y) |--> 0]
sage: r(t)=[cos(t),sin(t)]
sage: parametric_plot(r(t), (t,0,2*pi))

sage: # multivariable 2nd derivative test
sage: f(x,y)=x^2*y+y^2+y
sage: f.diff() # gradient
((x, y) |--> 2*x*y, (x, y) |--> x^2 + 2*y + 1)
sage: solve(list(f.diff()),[x,y])
[[x == -I, y == 0], [x == I, y == 0], [x == 0, y == (-1/2)]]
sage: f.diff(2)  # Hessian matrix
[(x, y) |--> 2*y (x, y) |--> 2*x]
[(x, y) |--> 2*x   (x, y) |--> 2]
sage: f.diff(2)(x=0,y=-1/2)
[-1  0]
[ 0  2]
sage: f.diff(2)(x=0,y=-1/2).eigenvalues()
[-1, 2]
sage: # we have a saddle point

CC: @mwhansen @burcin @kcrisman @rbeezer

Component: symbolics

Author: Jason Grout

Reviewer: Rob Beezer

Merged: sage-4.4.2.alpha0

Issue created by migration from https://trac.sagemath.org/ticket/8866

jasongrout commented 14 years ago
comment:1

Right now, all the docs are wrong or missing for the added functionality.

jasongrout commented 14 years ago

Attachment: trac-8866-preparse-vector-functions-derivatives.patch.gz

jasongrout commented 14 years ago
comment:2

I added docs for each functionality that changed.

jasongrout commented 14 years ago
comment:3

make ptestlong appears to pass on 4.4.1 (ubuntu 64-bit).

1d7ec08f-60ae-4512-91a6-8324c06eab9f commented 14 years ago
comment:4

I'm running tests right now. In the meantime, could this be less ugly? I realize that the naked '4's are not callable, so there is the deprecation warning, but the error warning seems severe.

sage: d=matrix(SR, [[4, 4]])
sage: d(3)
/sage/dev/local/lib/python2.6/site-packages/IPython/iplib.py:2073: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
  exec code_obj in self.user_global_ns, self.user_ns
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/sage/dev/<ipython console> in <module>()

/sage/dev/local/lib/python2.6/site-packages/sage/matrix/matrix_symbolic_dense.so in sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense.__call__ (sage/matrix/matrix_symbolic_dense.c:3956)()

ValueError: the number of arguments must be less than or equal to 0

I didn't know you could create a vector space of functions. Complete with a basis. ;-)

sage: g(x,y)=x^2+y^3
sage: g
(x, y) |--> x^2 + y^3
sage: grad=g.diff()
sage: grad
((x, y) |--> 2*x, (x, y) |--> 3*y^2)
sage: grad.parent()
Vector space of dimension 2 over Callable function ring with arguments (x, y)
sage: grad.parent().basis()
[
((x, y) |--> 1, (x, y) |--> 0),
((x, y) |--> 0, (x, y) |--> 1)
]
jasongrout commented 14 years ago
comment:5

Replying to @rbeezer:

I'm running tests right now. In the meantime, could this be less ugly? I realize that the naked '4's are not callable, so there is the deprecation warning, but the error warning seems severe.

That's stemming from this, of course:


sage: SR(4)(2)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/jason/<ipython console> in <module>()

/home/jason/sage/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.__call__ (sage/symbolic/expression.cpp:15477)()

/home/jason/sage/local/lib/python2.6/site-packages/sage/symbolic/ring.so in sage.symbolic.ring.SymbolicRing._call_element_ (sage/symbolic/ring.cpp:6523)()

ValueError: the number of arguments must be less than or equal to 0

I don't know what should be done about that to make it prettier. Usually you wouldn't "call" an integer by itself (as just a symbolic integer is not a function...)

Note that making matrices callable is just extending the existing behavior for vectors.

I didn't know you could create a vector space of functions. Complete with a basis. ;-)

Yes, interesting. That stems from callable expressions being just normal expressions with a bit of extra information (default variable order for calls). Of course, it gave you back a basis for symbolic expressions.

1d7ec08f-60ae-4512-91a6-8324c06eab9f commented 14 years ago
comment:6

Replying to @jasongrout:

I don't know what should be done about that to make it prettier.

Me either. ;-) I guess I found it odd that there was a deprecation warning, then a failure. But maybe that's just the way it goes.

1d7ec08f-60ae-4512-91a6-8324c06eab9f commented 14 years ago
comment:7

This all checks out fine: builds and runs, passes all tests, documentation is fine.

So, positive review.

7c09a680-e216-4024-bb8e-9bfd4aa7f313 commented 14 years ago

same as previous but with ticket number in commit message

7c09a680-e216-4024-bb8e-9bfd4aa7f313 commented 14 years ago
comment:8

Attachment: trac-8866-preparse-vector-functions-derivatives.2.patch.gz

7c09a680-e216-4024-bb8e-9bfd4aa7f313 commented 14 years ago

Reviewer: Rob Beezer

7c09a680-e216-4024-bb8e-9bfd4aa7f313 commented 14 years ago

Merged: sage-4.4.2.alpha0