Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preparse vector-valued functions and derivatives #8866

Closed
jasongrout opened this issue May 4, 2010 · 12 comments
Closed

preparse vector-valued functions and derivatives #8866

jasongrout opened this issue May 4, 2010 · 12 comments

Comments

@jasongrout
Copy link
Member

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
Copy link
Member Author

comment:1

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

@jasongrout
Copy link
Member Author

@jasongrout
Copy link
Member Author

comment:2

I added docs for each functionality that changed.

@jasongrout
Copy link
Member Author

comment:3

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

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented May 5, 2010

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
Copy link
Member Author

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.

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented May 6, 2010

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.

@rbeezer
Copy link
Mannequin

rbeezer mannequin commented May 6, 2010

comment:7

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

So, positive review.

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

same as previous but with ticket number in commit message

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

Reviewer: Rob Beezer

@sagetrac-mvngu
Copy link
Mannequin

sagetrac-mvngu mannequin commented May 8, 2010

Merged: sage-4.4.2.alpha0

@sagetrac-mvngu sagetrac-mvngu mannequin removed the s: positive review label May 8, 2010
@sagetrac-mvngu sagetrac-mvngu mannequin closed this as completed May 8, 2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants