Tutorial page 3: add more cwise operations, condense rest.

This commit is contained in:
Jitse Niesen
2010-07-12 22:45:57 +01:00
parent 8e776c94c1
commit 140ad0908d
9 changed files with 121 additions and 160 deletions

View File

@@ -8,17 +8,17 @@ int main()
{
ArrayXXf m(2,2);
//assign some values coefficient by coefficient
// assign some values coefficient by coefficient
m(0,0) = 1.0; m(0,1) = 2.0;
m(1,0) = 3.0; m(1,1) = 4.0;
m(1,0) = 3.0; m(1,1) = m(0,1) + m(1,0);
//print values to standard output
// print values to standard output
cout << m << endl << endl;
// using the comma-initializer is also allowed
m << 1.0,2.0,
3.0,4.0;
//print values to standard output
// print values to standard output
cout << m << endl;
}

View File

@@ -8,15 +8,16 @@ int main()
{
ArrayXXf a(3,3);
ArrayXXf b(3,3);
a << 1,2,3,
4,5,6,
7,8,9;
b << 1,2,3,
1,2,3,
1,2,3;
cout << "a + b = " << endl
<< a + b << endl;
// Adding two arrays
cout << "a + b = " << endl << a + b << endl << endl;
// Subtracting a scalar from an array
cout << "a - 2 = " << endl << a - 2 << endl;
}

View File

@@ -1,17 +0,0 @@
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
ArrayXXf a(3,3);
a << 1,2,3,
4,5,6,
7,8,9;
cout << "a + 2 = " << endl
<< a + 2 << endl;
}

View File

@@ -0,0 +1,19 @@
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
ArrayXf a = ArrayXf::Random(5);
a *= 2;
cout << "a =" << endl
<< a << endl;
cout << "a.abs() =" << endl
<< a.abs() << endl;
cout << "a.abs().sqrt() =" << endl
<< a.abs().sqrt() << endl;
cout << "a.min(a.abs().sqrt()) =" << endl
<< a.min(a.abs().sqrt()) << endl;
}

View File

@@ -8,31 +8,15 @@ int main()
{
MatrixXf m(2,2);
MatrixXf n(2,2);
MatrixXf result(2,2);
//initialize matrices
m << 1,2,
3,4;
n << 5,6,
7,8;
// mix of array and matrix operations
// first coefficient-wise addition
// then the result is used with matrix multiplication
result = (m.array() + 4).matrix() * m;
cout << "-- Combination 1: --" << endl
<< result << endl << endl;
// mix of array and matrix operations
// first coefficient-wise multiplication
// then the result is used with matrix multiplication
cout << "-- Combination 1: --" << endl << result << endl << endl;
result = (m.array() * n.array()).matrix() * m;
cout << "-- Combination 2: --" << endl
<< result << endl << endl;
cout << "-- Combination 2: --" << endl << result << endl << endl;
}

View File

@@ -1,34 +0,0 @@
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
using namespace std;
int main()
{
ArrayXXf m(2,2);
ArrayXXf n(2,2);
ArrayXXf result(2,2);
//initialize arrays
m << 1,2,
3,4;
n << 5,6,
7,8;
// --> array multiplication
result = m * n;
cout << "-- Array m*n: --" << endl
<< result << endl << endl;
// --> Matrix multiplication
result = m.matrix() * n.matrix();
cout << "-- Matrix m*n: --" << endl
<< result << endl << endl;
}

View File

@@ -8,34 +8,19 @@ int main()
{
MatrixXf m(2,2);
MatrixXf n(2,2);
MatrixXf result(2,2);
//initialize matrices
m << 1,2,
3,4;
n << 5,6,
7,8;
// --> matrix multiplication
result = m * n;
cout << "-- Matrix m*n: --" << endl
<< result << endl << endl;
// --> coeff-wise multiplication
cout << "-- Matrix m*n: --" << endl << result << endl << endl;
result = m.array() * n.array();
cout << "-- Array m*n: --" << endl
<< result << endl << endl;
// ->> coeff-wise addition of a scalar
cout << "-- Array m*n: --" << endl << result << endl << endl;
result = m.cwiseProduct(n);
cout << "-- With cwiseProduct: --" << endl << result << endl << endl;
result = m.array() + 4;
cout << "-- Array m + 4: --" << endl
<< result << endl << endl;
cout << "-- Array m + 4: --" << endl << result << endl << endl;
}

View File

@@ -8,13 +8,9 @@ int main()
{
ArrayXXf a(2,2);
ArrayXXf b(2,2);
a << 1,2,
3,4;
b << 5,6,
7,8;
cout << "a * b = " << endl
<< a * b << endl;
cout << "a * b = " << endl << a * b << endl;
}