ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 29 Jan 2021 01:25:10 +0100Matrix multiplication with a vector in EuclideanSpacehttps://ask.sagemath.org/question/55490/matrix-multiplication-with-a-vector-in-euclideanspace/ Hello
Oversimplified version of my question is "How can I multiply a vector with a matrix?"
E.<x,y,z>=EuclideanSpace(start_index=0)
vf=E.vector_field([x*y,x^2,z**2]) # the components are for testing purposes
M=Matrix(RR,3,3);M[:]=1 # this is just a test
# Neither this
M*vf
# nor this works
M*vf.at(E((3,2,1)))
The only work around I could think of is
v=vf.at(E((3,2,1)))
q=vector([v[0],v[1],v[2]])
M*q
or directly on the vector field
v=vector([v[0].expr(), v[1].expr(), v[2].expr()])
M*q
With this approach I have to put the transformed components back in the vector field defined within EuclideanSpace.
I would like to use EuclideanSpace to work with vectors.
Is there an easy way to handle this? Basically I am looking for an easy way to transform a vector field defined in EuclideanSpace?
Thanks in advance for your help.
curios_mindFri, 29 Jan 2021 01:25:10 +0100https://ask.sagemath.org/question/55490/NotImplementedErrorhttps://ask.sagemath.org/question/52972/notimplementederror/Hi
UBUNTU 18.04, SageMath 9.1, Jupyter Notebook
its ok when I set matrixForm=False, but I got **NotImplementedError** error when I choose matrixForm=True.
Why ?
var('r0_0,r0_1,r1_0,r1_1,p_0,p_1,v_0,v_1,x,y')
def projectvOnV(v,V) :
# projection matrix
x=V[0];y=V[1]
projectM=matrix(SR,[(x^2/(x^2 + y^2), x*y/(x^2 + y^2)),
(x*y/(x^2 + y^2), y^2/(x^2 + y^2))])
return projectM*v
matrixForm=True
if matrixForm :
Vr0 = matrix(SR,[r0_0,r0_1]).transpose()
Vr1 = matrix(SR,[r1_0,r1_1]).transpose()
Vp = matrix(SR,[p_0,p_1]).transpose()
else :
Vr0 = vector(SR,[r0_0,r0_1])
Vr1 = vector(SR,[r1_0,r1_1])
Vp = vector(SR,[p_0,p_1])
Vp=Vr0-Vr1
Vp_0=projectvOnV(Vr0,Vp)# vector Vr0 projeted on Vp
show("Vp : ",Vp,Vp.parent())
show("Vp_0 : ",Vp_0,Vp_0.parent())
Vin=Vr0-Vp_0
displayedObject=[Vin,Vr0,Vr1,Vp,Vp_0]
if matrixForm :
for e in displayedObject :
e=vector(e)
@interact
def _( thetaNumVr0=slider(0.01,2*pi,0.05,default=3.5),
rhoNumVr0=slider(0.01,4,0.05,default=2),
thetaNumVr1=slider(0.01,2*pi,0.05,default=5),
rhoNumVr1=slider(0.01,4,0.05,default=1)):
numL=[r0_0==rhoNumVr0*cos(thetaNumVr0),r0_1==rhoNumVr0*sin(thetaNumVr0),
r1_0==rhoNumVr1*cos(thetaNumVr1),r1_1==rhoNumVr1*sin(thetaNumVr1)]
plt=plot(arrow([0,0],Vr0.subs(numL),color="red",width=3,linestyle='dashed'))
plt+=text("Vr0",(Vr0*4/5+vector([0.1,0.1])).subs(numL),color="red",fontsize=10)
plt+=plot(arrow([0,0],Vr1.subs(numL),color="blue",width=3,linestyle='dashed'))
plt+=text("Vr1",(Vr1*4/5+vector([0,0.1])).subs(numL),color="blue",fontsize=10)
plt+=plot(arrow((Vr1).subs(numL).list(),(Vr1+Vp).subs(numL),color="grey",width=3,linestyle='dashed'))
plt+=text("Vp",((Vr1+Vp).subs(numL))/2,color="grey",fontsize=10)
plt+=plot(arrow([0,0],(Vp_0).subs(numL),color="pink",width=3,linestyle='dashed'))
plt+=text("Vp_0",(Vp_0*4/5+vector([0.1,0.1])).subs(numL),color="pink",fontsize=10)
plt+=plot(arrow([0,0],(Vin).subs(numL),color="black",width=3,linestyle='solid'))
plt+=text("Vin",(Vin*4/5+vector([0.1,0.1])).subs(numL),color="black",fontsize=10)
show(plt,aspect_ratio=1)ortolljWed, 12 Aug 2020 22:04:15 +0200https://ask.sagemath.org/question/52972/List Object not Callablehttps://ask.sagemath.org/question/52552/list-object-not-callable/ [link text](https://imgur.com/a/oGlsdV8)
Hello, see the link given above. Apparently, I need 60 points to upload images. So the idea is I am trying to construct a matrix by appending lists to each row. I am not understanding the error I am receiving.
CoefficientwList is just a list of numbers. From the second picture, you can see that Separation4 maps a list to a list of lists. By typing Separation4(CoefficientwList)[i1], I am extracting the i1th list in the list of lists. I then apply the vector function to make this list a vector and then from the 3rd picture, you see I add it to a row of the matrix M. Is there anything wrong here?
If more info is needed, please tell me but I am not seeing what the issue is. whatupmattSat, 18 Jul 2020 00:49:29 +0200https://ask.sagemath.org/question/52552/Checking what the span is for a vectorhttps://ask.sagemath.org/question/52485/checking-what-the-span-is-for-a-vector/Let's assume I have a vector called v1 and I have a matrix called Matrix.
Let us assume that the vector is in the span of the rows of Matrix. How would I know what the linear combination is? Here is what I do know. Let's assume this is a m by n matrix. I do know there is the span function. So
I can do something like make a list of vectors out of the Matrix. Say something like make an empty list ListofVectors=[].
i=0
while i< m:
ListofVectors.append(M[i])
i+=1
Now doing
v1 in span(ListofVectors)
will give me true assuming v1 is in the span. However, is there a function that tells me what the coefficients are for each term. For example, the vector v1=[3,2,1] for the ListofVectors being [1,1,1], [1,0,0], and [0,1,0] should give me coefficients 1,2,1 respectively as 1*[1,1,1] + 2*[1,0,0] + 1*[0,1,0] gives [3,2,1].
whatupmattThu, 16 Jul 2020 00:20:50 +0200https://ask.sagemath.org/question/52485/vector equation solvehttps://ask.sagemath.org/question/48532/vector-equation-solve/How to solve this?:
F = vector([cos(alpha),sin(alpha),z])
G = vector([z,cos(alpha),sin(alpha)])
A = vector([0,0,0])
solve(F-G == A)
Answer must be:
[cos(asin(cos(alpha))),
sin(asin(cos(alpha))),
sin(alpha)]dimonbavlyMon, 28 Oct 2019 10:39:17 +0100https://ask.sagemath.org/question/48532/Polynomial interpolation given a mathematic function f(x), an interval [a,b] and a natural number nhttps://ask.sagemath.org/question/43927/polynomial-interpolation-given-a-mathematic-function-fx-an-interval-ab-and-a-natural-number-n/Hi, i'm a computer science bachelor 's student and i'm having some trouble with this exercice, i'd like to get some help since it's my first course using sagemath, thanks, the exercice:
Write a function that takes as input a mathematical function f, an interval [a, b] and a natural n and return the interpolative polynomial of the function in the n + 1 points resulting from dividing the interval into subinterval n of equal length. That is to say, a polynomial p (x) of degree ≤ n such p (xi) = f (xi), where xi = a + i * ((b-a)/n) for all i = 0, ... n.
I'm stuck in the way of transforming a list into a matrix. This is what i've tried so far:
def list1(a,b,n):
r=(b-a)/n
return [a+i*r for i in range(n+1)]
def list2(a,b,n):
r=(b-a)/n
l=[]
for i in range(n+1):
l.append(a+i*r)
return l
def functionlist(f,llista):
return[f(i) for i in llista]
px=matrix(7, 1,list2(1, 5, 6))
show(px)
py=matrix(7, 1, functionlist(sin, list1(1, 5, 6)))
show(py)
points= zip(px,py); points
A=matrix(RDF,[[1,px[0],px[0]^2,px[0]^3],[1,px[1],px[1]^2,px[1]^3],[1,px[2],px[2]^2,px[2]^3],[1,px[3],px[3]^2,px[3]^3]]); A
And gives me this error:
NotImplementedError Traceback (most recent call last)
<ipython-input-112-a1670df9c827> in <module>()
19 points= zip(px,py); points
20
---> 21 A=matrix(RDF,[[Integer(1),px[Integer(0)],px[Integer(0)]**Integer(2),px[Integer(0)]**Integer(3)],[Integer(1),px[Integer(1)],px[Integer(1)]**Integer(2),px[Integer(1)]**Integer(3)],[Integer(1),px[Integer(2)],px[Integer(2)]**Integer(2),px[Integer(2)]**Integer(3)],[Integer(1),px[Integer(3)],px[Integer(3)]**Integer(2),px[Integer(3)]**Integer(3)]]); A
22
23
/opt/sagemath-8.3/local/lib/python2.7/site-packages/sage/modules/free_module_element.pyx in sage.modules.free_module_element.FreeModuleElement.__pow__ (build/cythonized/sage/modules/free_module_element.c:15842)()
2019 NotImplementedError
2020 """
-> 2021 raise NotImplementedError
2022
2023 def _repr_(self):
NotImplementedError:
Pls help, thanks.Hell_BoyFri, 12 Oct 2018 13:36:28 +0200https://ask.sagemath.org/question/43927/Quaternions Missing Important Functionality?https://ask.sagemath.org/question/34587/quaternions-missing-important-functionality/I use quaternions and Clifford algebras frequently for solving PDE boundary value problems as well as things like reflections, rotations etc. The underlying field for my quaternions is almost always either the complex numbers or the symbolic ring.
I am making an attempt to use them in Sage but I've run into a couple of obstacles that have given me pause. The absolute first things I looked for were,
1) scalar part or real part of the quaternion. Where is this function? I stumbled upon turning it into a list or vector but I presume that just taking the scalar part would be more efficient than dumping all the coefficients. For matrix representations it's often just the trace of the matrix. Correspondingly obtaining the vector part should be a standard function as well.
2) Quaternion automorphisms and anti-automorphisms? where are they? Yes we have conjugate which is both a method and an external function but what about the reversion and involution and the many other automorphisms? I don't expect to have to multiply by a bunch of unit vectors to get them for efficiency reasons. Also how do I distinguish between the quaternion conjugate and the complex conjugate of each of it's elements? This is a very important distinction that I would not know how to do without stripping out it's components and then remapping it.
3) Constructing a quaternion from coefficients. The only examples I've seen require explicit multiplications like
q = q0 + q1 * i + q2 * j + q3 * k . That doesn't seem efficient.
4) quaternions as elements of enclosing matrices and vectors. This would be very helpful since you could generate any Clifford algebra with this and it's often easier to analyze the components of said algebra that are isomorphic to Quaternions or Biquaternions in the complex case. Moreover I need to be able to do this for quaternions over the symbolic ring. It fails for me as this example from sage 7.3 shows:
<pre><code>
Q.<e1,e2,e3> = QuaternionAlgebra(SR, -1,-1)
var('x y z', domain='real')
q1 = x + x * I * e1
q2 = x - x * I * e1
v = vector([q1,q2])
</code></pre>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_36.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("US48ZTEsZTIsZTM+ID0gUXVhdGVybmlvbkFsZ2VicmEoU1IsIC0xLC0xKQp2YXIoJ3ggeSB6JywgZG9tYWluPSdyZWFsJykKcTEgPSB4ICsgeCAqIEkgKiBlMQpxMiA9IHggLSB4ICogSSAqIGUxCnYgPSB2ZWN0b3IoW3ExLHEyXSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpQ0ibdW/___code___.py", line 7, in <module>
exec compile(u'v = vector([q1,q2]) File "", line 1, in <module>
File "sage/modules/free_module_element.pyx", line 510, in sage.modules.free_module_element.vector (/data/AppData/SageMath/src/build/cythonized/sage/modules/free_module_element.c:5811)
TypeError: unsupported operand type(s) for ** or pow(): 'QuaternionAlgebra_ab' and 'int'
5) quaternion rotations. Most libraries will generate the unit quaternion that rotates in 3D space and/or have a function that applies it efficiently.
There are probably a few other things like generating a canonical matrix representation for a full quaternion and so forth. I actually wonder if the default quaternion package is the right tool for a physicist or an engineer or someone who wants to play around with the so called geometric algebra of Hestenes. It seems the perogatives of the scientist vs the algebraist are very very different. How efficient is this library? What if I had to multiply millions of quaternions? I'm guessing I'd be better off mapping it to complex matrices and invoking blas.
While most of the functionality can be added on via a little python programming, I am concerned about efficiency and consistency. Moreover item 4 is a bit of a quandary since it involves overloading a bunch of arithmetic operators and some new python classes and so forth.
I'm hoping someone will tell me that it's all there, I just wasn't able to find it in the documentation or so forth. Thanks for any help in advance.doomFri, 26 Aug 2016 04:11:21 +0200https://ask.sagemath.org/question/34587/scalar^[vector]?https://ask.sagemath.org/question/29814/scalarvector/I feel really stupid like this should be obvious, but I can't figure out how to do it.
v = vector([1,2,3])
2^v
should give me
[2, 4, 8]
I can't find any other way to do this without writing a function.drThu, 08 Oct 2015 03:00:40 +0200https://ask.sagemath.org/question/29814/How can I get an invertible matrix X with integral entries and AX=XB, where A and B are matrices with integral entries.https://ask.sagemath.org/question/24467/how-can-i-get-an-invertible-matrix-x-with-integral-entries-and-axxb-where-a-and-b-are-matrices-with-integral-entries/Let M be the set of all n by n matrices with integral entries.
For A and B in M, how can I get an invertible matrix X in M with AX=XB in Sage.
I can partially solve that problem in GAP following method.
- V={ X in Mn(QQ) | AX=XB }
(V is a vector space over QQ)
- Basis(V)={B_1,B_2, ..., B_k}
(I wonder which number k it is according to the changes of A and B.)
- Make X=a_1 * B_1 + ... +a_k * B_k , a_i in some interval in ZZ.
- Check two things which are X in Mn(ZZ) and the existence of the inverse of X in Mn(ZZ).
I can find such X for some easy matrices A,B.
How can I solve this problem in Sage?
Thanks.SeminSun, 12 Oct 2014 08:09:43 +0200https://ask.sagemath.org/question/24467/Yet another linear combinationhttps://ask.sagemath.org/question/10791/yet-another-linear-combination/Hi, I'm really new to Sage and to programming in general. I have 20 linear independent vectors of length 20, and a linear dependent vector. I would like to write this one as a linear combination of the others: I looked up on the internet all day, but nothing I found worked. The ways I tried are:
- define a vector space of dim 20 on the field I'm using, impose my vectors as a base, and use the method .coodinates(). PROBLEM: I couldn't impose the basis, I didn't find a command to do so.
- use G.solve_right(s), where G is a 20x20 matrix and s is my linear dependent vector. I thought I would have a vector as an output but instead I get something of dimension 20x8. Weird.
- I tried to do a linear system directly using equations with the vectors, in order to solve them with respect to some variables, but I got as an output that you can't do it using vectors.
So, what shall I try?
Thank you very much BubusetteteFri, 29 Nov 2013 18:49:42 +0100https://ask.sagemath.org/question/10791/How to assign matrix (vector) value to variablehttps://ask.sagemath.org/question/10599/how-to-assign-matrix-vector-value-to-variable/when I tried to assign a matrix or a vector to variables, something wrong happens:
sage: sin([1,2,3,4]).n()
Traceback (click to the left of this block for traceback)
...
TypeError: cannot coerce arguments: no canonical coercion from <type
'list'> to Symbolic Ring
what I want to achieve is that the out put gives a vector that equals to [sin(1),sin(2),sin(3),sin(4)].
Thanks in advance!chenmingTue, 08 Oct 2013 21:14:44 +0200https://ask.sagemath.org/question/10599/Can I define an n-dimensional matrix?https://ask.sagemath.org/question/10244/can-i-define-an-n-dimensional-matrix/I'm sure this is a basic question that has been asked before but I'm too stupid to find it.
What I'd like to do is something like this
k=var('n')
assume(n, 'integer')
assume(n>0)
VS = MatrixSpace(SR, n, 1)
to get the space of all *n*×1 matrices, i.e. column vectors. Is it at all possible to define a generalized *n*-dimensional vector or *n×n* matrix? Or am I just taking the completely wrong approach here?
**Edit:** Forgot to mention that the error I get is
ValueError: cannot convert n to int
mudd1Sat, 15 Jun 2013 15:52:18 +0200https://ask.sagemath.org/question/10244/