type problem, I think

33 views
Skip to first unread message

Jason Miller

unread,
Feb 11, 2022, 10:51:17 AM2/11/22
to sage-support
I'm trying to do some least squares linear algebra. I built a matrix from lists that I converted into column vectors and used the 'augment' function.  I used the matrix to compute a least squares solution, and I have an solution vector, but it's type is making my life difficult.

When I type() an entry in the vector, I get:

'sage.modules.free_module_element.FreeModuleElement_generic_dense'

I want to pull the entries of this vector and use them as coefficients in a polynomial. My solution is the 3-vector I called x. This attempt at defining a quadratic

s=var('s')
f(s)=x[0]*s^2+x[1]*s+x[2]
plot(f(s),(s,0,2))

gives me this error

Error in lines 2-2 Traceback (most recent call last): File "/cocalc/lib/python3.9/site-packages/smc_sagews/sage_server.py", line 1230, in execute exec( File "", line 1, in <module> File "/ext/sage/9.4/local/lib/python3.9/site-packages/sage/calculus/all.py", line 170, in symbolic_expression return SR(x) File "sage/structure/parent.pyx", line 898, in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9338) return mor._call_(x) File "sage/structure/coerce_maps.pyx", line 161, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4622) raise File "sage/structure/coerce_maps.pyx", line 156, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4514) return C._element_constructor(x) File "sage/symbolic/ring.pyx", line 416, in sage.symbolic.ring.SymbolicRing._element_constructor_ (build/cythonized/sage/symbolic/ring.cpp:7464) raise TypeError(f"unable to convert {x!r} to a symbolic expression") TypeError: unable to convert (-4.87852077236756*s^2 + 19.9617665490168*s + 4.00810430656697) to a symbolic expression

I feel like I'm in type hell. Can someone help me get back into the right Universe for this work? My scouring of Google hasn't turned up anything helpful.

Jason

slelievre

unread,
Feb 11, 2022, 12:42:54 PM2/11/22
to sage-support
Please provide complete code to reproduce. The following works for me.
```
sage: x = vector((4.00810430656697, 19.9617665490168, -4.87852077236756))
sage: f(s) = x[0] * s^0 + x[1] * s^1 + x[2] * s^2
sage: plot(f(s), (s, 0, 2))
Launched png viewer for Graphics object consisting of 1 graphics primitive
```

Nils Bruin

unread,
Feb 11, 2022, 1:59:52 PM2/11/22
to sage-support
On Friday, 11 February 2022 at 07:51:17 UTC-8 mille...@gmail.com wrote:
I'm trying to do some least squares linear algebra. I built a matrix from lists that I converted into column vectors and used the 'augment' function.

Small detail, but slightly more efficient (it may not matter for your application) is to just give sage a list of lists and create a matrix from that directly. It will cost less copying around than iteratively augmenting the matrix. For the most part, matrices are stored row-major (i.e., basically as an array of rows), so column augmentations require more fiddling internally than row operations. Better to just construct the matrix from rows and then transpose if required.

When I type() an entry in the vector, I get:

'sage.modules.free_module_element.FreeModuleElement_generic_dense'

Important part is actually the *parent* of the vector, and more specifically its *base ring*, i.e., ask v.base_ring()

In your case, I think the base ring should be some type of real field (if you're doing least-squares ...). If it is the symbolic ring, you probably want to change that first.

To convert a vector to a polynomial with coefficients equal to the entries in the vector, you can do:

R=v.base_ring()
R['s'](v)
 
(or, if you want the leading term to be the leftmost entry, i.e., a reversed vector):

R['s'](v[::-1])

or

R['s'](reversed(M['0']))

If you're going to be using the ring R['s'] a lot, it will be better to define

Rs.<s>=R[]

and use

Rs(v).

Reply all
Reply to author
Forward
0 new messages