Discussion:
Cramer's rule using LinearAlgebra package?
(too old to reply)
a***@gmail.com
2005-10-05 07:43:06 UTC
Permalink
Using the older linalg package, I could implement Cramer's rule, given
A1:=copyinto(b,copy(A),1,1);
det(A1)/det(A);
But I can't work out how to do something similar with the newer
A1:=A;
A1[1..4,1]:=b;
(for a 4x4 system). But this changes both A1 and A, so that
Determinant(A1)/Determinant(A);
produces 1. If I assign A1:=copy(A) the same thing happens. Clearly I
want to write the vector b into a column of a copy of A, without
changing the original matrix A. So how do I do this?

cheers,
Alasdair
a***@gmail.com
2005-10-05 08:01:09 UTC
Permalink
Scratch that - I don't know what was going on in my tiny mind:
A1:=copy(A) works fine, as it should.

-A.
Nasser Abbasi
2005-10-05 10:27:17 UTC
Permalink
Post by a***@gmail.com
A1:=copy(A) works fine, as it should.
-A.
I just started learning the Linear Algebra package myself, (I am doing
work on control systems so I would need to learn it really well :) it
seems like a nice package.

so as an exercise I implemented the cramer rule, used the Copy()
Post by a***@gmail.com
restart;
A:=Matrix([ [4,-1,1],[2,2,3],[5,-2,6] ]);
b:=Vector([-5,10,1]);
d:=Determinant(A);
nRow,nCol:=Dimension(A);
for i from 1 to nCol do
'solution'=evalf(Determinant(A1)/d);
end do;
The solutions came up to be -1,3,2

Nasser
Carl Love
2005-10-05 13:34:11 UTC
Permalink
Post by a***@gmail.com
Using the older linalg package, I could implement Cramer's rule, given
A1:=copyinto(b,copy(A),1,1);
det(A1)/det(A);
But I can't work out how to do something similar with the newer
LinearAlgebra package.
The copying can be done implicitly using the matrix construction and
indexing shortcuts:


Cramer:= proc(A::Matrix, b::{Vector,Matrix})
::Vector;
local i::posint;
uses LinearAlgebra;
map(
`*`
,<seq(
Determinant(<A[1..-1, 1..i-1] | b | A[1..-1, i+1..-1]>)
,i= 1..Dimension(A)[2])
,1/Determinant(A)
)
end proc:

An example of its use:

Cramer(<<4,2,5> | <-1,2,-2> | <1,3,6>>, <-5,10,1>);

[-1]
[ ]
[ 3]
[ ]
[ 2]
Nasser Abbasi
2005-10-05 13:54:46 UTC
Permalink
Post by Carl Love
Post by a***@gmail.com
Using the older linalg package, I could implement Cramer's rule, given
A1:=copyinto(b,copy(A),1,1);
det(A1)/det(A);
But I can't work out how to do something similar with the newer
LinearAlgebra package.
The copying can be done implicitly using the matrix construction and
Cramer:= proc(A::Matrix, b::{Vector,Matrix})
::Vector;
local i::posint;
uses LinearAlgebra;
map(
`*`
,<seq(
Determinant(<A[1..-1, 1..i-1] | b | A[1..-1, i+1..-1]>)
,i= 1..Dimension(A)[2])
,1/Determinant(A)
)
Cramer(<<4,2,5> | <-1,2,-2> | <1,3,6>>, <-5,10,1>);
[-1]
[ ]
[ 3]
[ ]
[ 2]
Carl;

This is what I was trying to do at first (i.e. construct the matrix on
the fly), but when I was writing

A[1..-1, 1..i-1]

and when 'i' was 1 (start of seq() ), Maple did not like

A[1..-1, 1..0]

so that is why I gave up on constructing it on the stack (even though
I knew it could be done)

I need to find out why it worked with you and not me :)

because when I just write A[1..-1, 1..0] as a separate statement by
itself, maple does not like. Strange that it seems to work as you
wrote it.

Nasser
Preben Alsholm
2005-10-05 15:10:02 UTC
Permalink
Nasser Abbasi wrote:
when I just write A[1..-1, 1..0] as a separate statement by
Post by Nasser Abbasi
itself, maple does not like.
A[1..-1, 1..0];
You see a matrix with 5 rows and no columns
Post by Nasser Abbasi
Dimensions(%);
5, 0
Post by Nasser Abbasi
<%%|<1,2,3,4,5>>;
You see a matrix with 5 rows and 1 column.

Preben Alsholm
Carl Love
2005-10-05 15:22:33 UTC
Permalink
Post by Nasser Abbasi
This is what I was trying to do at first (i.e. construct the matrix on
the fly), but when I was writing
A[1..-1, 1..i-1]
and when 'i' was 1 (start of seq() ), Maple did not like
A[1..-1, 1..0]
so that is why I gave up on constructing it on the stack (even though
I knew it could be done) I need to find out why it worked with you and
not me :) because when I just write A[1..-1, 1..0] as a separate
statement by itself, maple does not like. Strange that it seems to work
as you wrote it.
I tried the following in Maple 6, 9.5 & 10. In all cases, it returned
the empty Matrix, which is the correct behavior.

restart;
A:= <<1,2>|<3,4>>:
B:= A[1..-1, 1..0];
B:= MATRIX([[],[]])

What version of Maple are you using? Could you try the above
statements exactly as presented?
Nasser Abbasi
2005-10-05 17:04:17 UTC
Permalink
Post by Carl Love
Post by Nasser Abbasi
This is what I was trying to do at first (i.e. construct the matrix on
the fly), but when I was writing
A[1..-1, 1..i-1]
and when 'i' was 1 (start of seq() ), Maple did not like
A[1..-1, 1..0]
so that is why I gave up on constructing it on the stack (even though
I knew it could be done) I need to find out why it worked with you and
not me :) because when I just write A[1..-1, 1..0] as a separate
statement by itself, maple does not like. Strange that it seems to work
as you wrote it.
I tried the following in Maple 6, 9.5 & 10. In all cases, it
returned
the empty Matrix, which is the correct behavior.
restart;
B:= A[1..-1, 1..0];
B:= MATRIX([[],[]])
What version of Maple are you using? Could you try the above
statements exactly as presented?
I am getting the same thing.
I have no idea what I was doing now, if I find out will let you know.

thanks,
Nasser

Loading...