Created
May 5, 2017 06:52
-
-
Save kurenaif/206ca4502062f117000e9b8602a0b420 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate nalgebra as na; //行列とかベクトルとかのライブラリ | |
fn main(){ | |
// 多分DMatrixのほうが使いやすい? staticは定数な印象 | |
let mat = na::DMatrix::from_iterator(3,3,[ | |
1.,2.,3., | |
4.,5.,6., | |
7.,8.,9. | |
].iter().cloned()); | |
let mat2 = na::DMatrix::from_iterator(3,3,[ | |
1.,3.,5., | |
2.,3.,8., | |
2.,1.,5. | |
].iter().cloned()); | |
let v = na::Vector3::new(1, 2, 3); | |
let dm1 = na::DMatrix::from_diagonal_element(3, 3, 2.0); | |
let dm2 = na::DMatrix::from_diagonal_element(3, 3, 2.0); | |
println!("- - - mat - - -\n{}",mat); // DMatrixだと表示がいい感じになる | |
// println!("- - - inv(mat) - - -\n{}",mat.try_inverse().unwrap()); // panicked at 'called `Option::unwrap()` on a `None` value', | |
println!("- - - mat2 - - -\n{}",mat2); | |
println!("- - - inv(mat2) - - - \n{}",mat2.try_inverse().unwrap()); //inverce matrix Optionalがついてるのでunwrap()で取る. | |
// http://www.geisya.or.jp/~mwm48961/linear_algebra/simul_eq1.htm の 例1 を解く | |
println!("========================= solve equations !!! ========================="); | |
let a = na::DMatrix::from_iterator(3,3,[ | |
1.,1.,0., | |
2.,-1.,1., | |
3.,2.,1. | |
].iter().cloned()); | |
println!("A\n{}",a); | |
let b = na::DVector::from_iterator(3,[ | |
0.,3.,-1. | |
].iter().cloned()); | |
println!("B\n{}",b); | |
let invA = a.try_inverse().unwrap(); | |
println!("invA\n{}",invA); | |
println!("x\n{}", invA*b) | |
} | |
/* output | |
- - - mat - - - | |
┌ ┐ | |
│ 1.000 4.000 7.000 │ | |
│ 2.000 5.000 8.000 │ | |
│ 3.000 6.000 9.000 │ | |
└ ┘ | |
- - - mat2 - - - | |
┌ ┐ | |
│ 1.000 2.000 2.000 │ | |
│ 3.000 3.000 1.000 │ | |
│ 5.000 8.000 5.000 │ | |
└ ┘ | |
- - - inv(mat2) - - - | |
┌ ┐ | |
│ 1.400 1.200 -0.800 │ | |
│ -2.000 -1.000 1.000 │ | |
│ 1.800 0.400 -0.600 │ | |
└ ┘ | |
========================= solve equations !!! ========================= | |
A | |
┌ ┐ | |
│ 1.000 2.000 3.000 │ | |
│ 1.000 -1.000 2.000 │ | |
│ 0.000 1.000 1.000 │ | |
└ ┘ | |
B | |
┌ ┐ | |
│ 0.000 │ | |
│ 3.000 │ | |
│ -1.000 │ | |
└ ┘ | |
invA | |
┌ ┐ | |
│ 1.500 -0.500 -3.500 │ | |
│ 0.500 -0.500 -0.500 │ | |
│ -0.500 0.500 1.500 │ | |
└ ┘ | |
x | |
┌ ┐ | |
│ 2.000 │ | |
│ -1.000 │ | |
│ 0.000 │ | |
└ ┘ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment