Ask Your Question
2

1x1 matrix -> scalar

asked 2016-01-19 05:05:44 -0500

thetha gravatar image

updated 2016-01-19 11:05:33 -0500

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/1...

Thank you in advance.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-01-19 06:09:55 -0500

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*p
edit flag offensive delete link more

Comments

Thank you is there a short cut with brackets to create vectors and matrices?

thetha gravatar imagethetha ( 2016-01-20 03:43:22 -0500 )edit

Yes 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.

vdelecroix gravatar imagevdelecroix ( 2016-01-20 07:34:01 -0500 )edit
1

answered 2016-01-19 06:31:25 -0500

ndomes gravatar image

updated 2016-01-19 08:02:46 -0500

1 x 1 matrix A --> scalar a :

a =  A[0,0]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-01-19 05:05:44 -0500

Seen: 228 times

Last updated: Jan 19 '16