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.Wed, 20 Jan 2016 14:34:01 +01001x1 matrix -> scalarhttps://ask.sagemath.org/question/32288/1x1-matrix-scalar/Hi everyone, i have a problem with dimensions:
I am programing the conjugate gradient algorithm,
unfortunatly, when the result is scalar, Sage still handles it a 1x1 matrix,
and as a result i cannot use multiplication. How can i fix it, maybe there is a good tutorial or a book
for Sage and linear algebra.
Here is my work so far:
https://cloud.sagemath.com/projects/1d25492e-4517-4c0f-99c6-b1205afc8edf/files/2016-01-18-112346.sagews
Thank you in advance.Tue, 19 Jan 2016 12:05:44 +0100https://ask.sagemath.org/question/32288/1x1-matrix-scalar/Answer by ndomes for <p>Hi everyone, i have a problem with dimensions:
I am programing the conjugate gradient algorithm,
unfortunatly, when the result is scalar, Sage still handles it a 1x1 matrix,
and as a result i cannot use multiplication. How can i fix it, maybe there is a good tutorial or a book
for Sage and linear algebra.</p>
<p>Here is my work so far:</p>
<p><a href="https://cloud.sagemath.com/projects/1d25492e-4517-4c0f-99c6-b1205afc8edf/files/2016-01-18-112346.sagews">https://cloud.sagemath.com/projects/1...</a></p>
<p>Thank you in advance.</p>
https://ask.sagemath.org/question/32288/1x1-matrix-scalar/?answer=32290#post-id-322901 x 1 matrix A --> scalar a :
a = A[0,0]
Tue, 19 Jan 2016 13:31:25 +0100https://ask.sagemath.org/question/32288/1x1-matrix-scalar/?answer=32290#post-id-32290Answer by vdelecroix for <p>Hi everyone, i have a problem with dimensions:
I am programing the conjugate gradient algorithm,
unfortunatly, when the result is scalar, Sage still handles it a 1x1 matrix,
and as a result i cannot use multiplication. How can i fix it, maybe there is a good tutorial or a book
for Sage and linear algebra.</p>
<p>Here is my work so far:</p>
<p><a href="https://cloud.sagemath.com/projects/1d25492e-4517-4c0f-99c6-b1205afc8edf/files/2016-01-18-112346.sagews">https://cloud.sagemath.com/projects/1...</a></p>
<p>Thank you in advance.</p>
https://ask.sagemath.org/question/32288/1x1-matrix-scalar/?answer=32289#post-id-32289 Hello
Of course you can not multiply 1x1 matrices with a 3x1 matrix... You should use vectors instead of matrix columns (they are considered as the same time as row and column vector and hence no need to transpose). The following works
B = Matrix([[-1,0,1], [0,-1,1], [1,0,1], [0,1,1], [-1,0,1]])
A = B.transpose()*B
c = vector([0,0,2,2,0]) # <- changed to vector
b = B.transpose()*c
x = vector([1,0,1]) # <- changed to vector
r = b-A*x
p = r
a = ((r.norm())^2)/(p*A*p) # <- removed transpose
x = x+a*pTue, 19 Jan 2016 13:09:55 +0100https://ask.sagemath.org/question/32288/1x1-matrix-scalar/?answer=32289#post-id-32289Comment by thetha for <p>Hello</p>
<p>Of course you can not multiply 1x1 matrices with a 3x1 matrix... You should use vectors instead of matrix columns (they are considered as the same time as row and column vector and hence no need to transpose). The following works</p>
<pre><code>B = Matrix([[-1,0,1], [0,-1,1], [1,0,1], [0,1,1], [-1,0,1]])
A = B.transpose()*B
c = vector([0,0,2,2,0]) # <- changed to vector
b = B.transpose()*c
x = vector([1,0,1]) # <- changed to vector
r = b-A*x
p = r
a = ((r.norm())^2)/(p*A*p) # <- removed transpose
x = x+a*p
</code></pre>
https://ask.sagemath.org/question/32288/1x1-matrix-scalar/?comment=32294#post-id-32294Thank you
is there a short cut with brackets to create vectors and matrices?Wed, 20 Jan 2016 10:43:22 +0100https://ask.sagemath.org/question/32288/1x1-matrix-scalar/?comment=32294#post-id-32294Comment by vdelecroix for <p>Hello</p>
<p>Of course you can not multiply 1x1 matrices with a 3x1 matrix... You should use vectors instead of matrix columns (they are considered as the same time as row and column vector and hence no need to transpose). The following works</p>
<pre><code>B = Matrix([[-1,0,1], [0,-1,1], [1,0,1], [0,1,1], [-1,0,1]])
A = B.transpose()*B
c = vector([0,0,2,2,0]) # <- changed to vector
b = B.transpose()*c
x = vector([1,0,1]) # <- changed to vector
r = b-A*x
p = r
a = ((r.norm())^2)/(p*A*p) # <- removed transpose
x = x+a*p
</code></pre>
https://ask.sagemath.org/question/32288/1x1-matrix-scalar/?comment=32297#post-id-32297Yes and no. You can do the following
sage: V = ZZ^4
sage: V([1,2,5,-1])
(1, 2, 5, -1)
But there can not be a shortcut in Sage and while being compatible with Python. Namely brackets are used for lists and paranthesis for tuples.Wed, 20 Jan 2016 14:34:01 +0100https://ask.sagemath.org/question/32288/1x1-matrix-scalar/?comment=32297#post-id-32297