Ask Your Question
2

1x1 matrix -> scalar

asked 2016-01-19 12:05:44 +0100

thetha gravatar image

updated 2016-01-19 18:05:33 +0100

slelievre gravatar image

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
2

answered 2016-01-19 13:31:25 +0100

ndomes gravatar image

updated 2016-01-19 15:02:46 +0100

1 x 1 matrix A --> scalar a :

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

answered 2016-01-19 13:09:55 +0100

vdelecroix gravatar image

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 10:43:22 +0100 )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 14:34:01 +0100 )edit

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 12:05:44 +0100

Seen: 1,911 times

Last updated: Jan 19 '16