Forum Numerical Method Linear Algebra
create a diagonal matrix
-
osigrtoeltParticipant 2010-12-16 at 10:00 am
What is the best way to create an identity matrix in SuanShu? How about other diagonal matrices? Thanks.
-
Haksun LiModerator 2010-12-16 at 10:08 am
From any [tt:v42epeki]DenseMatrix[/tt:v42epeki] A, you can create the identity matrix by calling
<br /> Matrix I = A.ONE();<br />If you don’t already have a matrix but know the dimension, you can do
<br /> Matrix I = new DenseMatrix(2, 2).ONE();<br />To create a diagonal matrix from another matrix A, you can do
<br /> Matrix D = CreateMatrix.diagonalMatrix(A);<br /> -
osigrtoeltParticipant 2010-12-16 at 10:13 am
What should I do if I would to create a 5 by 5 diagonal matrix with entries {1, 2, 3, 4, 5} on the main diagonal?
-
Haksun LiModerator 2010-12-16 at 1:53 pm
<br /> DiagonalMatrix instance = new DiagonalMatrix(new double[]{1, 2, 3, 4, 5});<br /> -
osigrtoeltParticipant 2010-12-17 at 2:35 am
How do we coerce a Matrix to a DenseMatrix?
For instance,
Matrix I = new DenseMatrix(2, 2).ONE();I becomes a Matrix and I.setColumn() doesn’t seem to work.
Any help would be greatly appreciated.
-
Haksun LiModerator 2010-12-17 at 5:49 am
Maybe declare I as a [tt:3g6j7frh]DenseMatrix[/tt:3g6j7frh] to start with?
<br /> DenseMatrix I = new DenseMatrix(2, 2).ONE();<br /> -
osigrtoeltParticipant 2010-12-17 at 7:31 am
No, that yields ‘incompatible types’, since DenseMatrix(int, int).ONE() is of type Matrix. Please correct me if I’m wrong.
I’m currently doing something like this, but it seems cumbersome:
DenseMatrix I = new DenseMatrix(new DenseMatrix(2, 2).ONE());