Opened 9 years ago

Closed 9 years ago

# jacobian fails for pynac expressions

Reported by: Owned by: burcin burcin major sage-4.0 symbolics wstein, mhansen, robertwb

### Description

Reported by Alex Raichev on sage-support:

----------------------------------------------------------------------
| Sage Version 3.4, Release Date: 2009-03-11                         |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: var('x,y', ns=1)
(x, y)
sage: f= x+y
sage: type(f)
<type 'sage.symbolic.expression.Expression'>
sage: jacobian(f,[x,y])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call
last)

/Users/arai021/<ipython console> in <module>()

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
functions.pyc in jacobian(functions, variables)
136
137     if not isinstance(variables, (tuple, list)) and not
is_Vector(variables):
138         variables = [variables]
139
--> 140     return matrix([[diff(f, v) for v in variables] for f in
functions])

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
functional.pyc in derivative(f, *args, **kwds)
145         pass
146     if not isinstance(f, SymbolicExpression):
--> 147         f = SR(f)
148     return f.derivative(*args, **kwds)
149

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
calculus.pyc in __call__(self, x)
504                 msg, s, pos = err.args
505                 raise TypeError, "%s: %s !!! %s" % (msg, s
[:pos], s[pos:])
--> 506         return self._coerce_impl(x)
507
508     def _coerce_impl(self, x):

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
calculus.pyc in _coerce_impl(self, x)
566             return self(x._sage_())
567         else:
--> 568             raise TypeError, "cannot coerce type '%s' into a
SymbolicExpression."%type(x)
569
570     def _repr_(self):

TypeError: cannot coerce type '<type
'sage.symbolic.expression.Expression'>' into a SymbolicExpression.

sage.symbolic.expression.Expression doesn't support .derivative(), and the interface to .diff() doesn't match the Sage conventions.

### comment:1 Changed 9 years ago by burcin

• Status changed from new to assigned
• Summary changed from jacobian fails for pynac expressions to [with patch, needs review] jacobian fails for pynac expressions

Attached patches allow forming matrices and vectors with pynac expressions, and make them use the multi_derivative framework. This makes the jacobian command in the example above work.

### Changed 9 years ago by burcin

allow forming matrices and vectors from pynac expressions

### Changed 9 years ago by burcin

make pynac expressions use the multi_derivative framework

### comment:2 Changed 9 years ago by ncalexan

• Summary changed from [with patch, needs review] jacobian fails for pynac expressions to [with patch, needs work] jacobian fails for pynac expressions

With these two patches applied, you can get some wild things:

sage: var('x,y,z',ns=1)
(x, y, z)
sage: M = matrix(2,2,[x,y,z,x])
sage: M.base_ring()
New Symbolic Ring
sage: v = vector([x,y])
sage: v.base_ring()
New Symbolic Ring
sage: M * v
Exception exceptions.TypeError: 'mutable matrices are unhashable' in 'sage.modules.free_module_element.FreeModuleElement._cmp_same_ambient_c' ignored
<ERROR: mutable matrices are unhashable>
(([x y]
[z x])*x, ([x y]
[z x])*y)
sage: v * M
(x^2 + y*z, 2*x*y)
sage: M * v
<ERROR: mutable matrices are unhashable>
(([x y]
[z x])*x, ([x y]
[z x])*y)
sage: v * M * v
2*x*y^2 + (x^2 + y*z)*x
sage: v * (M * v)
/Users/ncalexan/.sage/temp/dhcp_v007_000.mobile.uci.edu/36399/_Users_ncalexan__sage_init_sage_0.py:1: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
# -*- coding: utf-8 -*-
([x y] + ([x y]

### Changed 9 years ago by burcin

patch to pynac to handle exceptions during hashing

### Changed 9 years ago by burcin

doctests for the fix

### comment:3 Changed 9 years ago by burcin

attachment:pynac-hash_exception.patch fixes exception handling during hashing in pynac and the problems reported by Nick in comment:2.

I will hold off on doing another pynac release to address this, since I don't know if mhansen made any changes which might be affected.

BTW, another pynac release, 0.1.6 in this case, would introduce circular dependencies in trac. :)

### comment:4 Changed 9 years ago by was

I'd just like to remark that Mike Hansen is probably the best person I've ever met at using rebasing patches and using revision control systems. With him, I would not be worried about doing something that conflicts with what he has done or with circular trac dependencies.

### comment:5 Changed 9 years ago by burcin

• Summary changed from [with patch, needs work] jacobian fails for pynac expressions to [with patch, needs review] jacobian fails for pynac expressions

The necessary changes for pynac, i.e., attachment:pynac-hash_exception.patch, is included in the new package for pynac-0.1.6 at #5777. The doctests in attachment:5546_doctests.patch are also posted in #5777.

This now introduces a circular dependency in trac. I hope this won't cause problems, since all these patches will go in together. Faster reviews in the future may prevent this from happening again.

Note that only:

needs to be applied from this ticket.

### comment:6 Changed 9 years ago by burcin

• Milestone changed from sage-4.0.1 to sage-4.0

Some variants of these patches seem to be in 4.0.rc0. This bug should be closed. Trac doesn't allow me to close bugs any more though.

### comment:7 Changed 9 years ago by mhansen

• Resolution set to fixed
• Status changed from assigned to closed
• Summary changed from [with patch, needs review] jacobian fails for pynac expressions to jacobian fails for pynac expressions

This was fixed by #5777 and #5390

Note: See TracTickets for help on using tickets.