# 1.5.3.2 Matrix Object Data Manipulation

## Set Values by Formula

The DataObject::SetFormula and DataObject::ExecuteFormula methods are used to set column/matrix values, which is the same as setting values in the Set Values dialog. The example below shows how to set values to a matrix object by formula.

// new a matrix window
MatrixPage matPage;
matPage.Create("Origin");
MatrixLayer ml = matPage.Layers(); // get active matrix sheet

// set formula and execute
MatrixObject mo = ml.MatrixObjects(0); //get first matrixobject
mo.SetFormula("sin(i) + cos(j)");
mo.ExecuteFormula();

## Copy Matrix Data

The matobj_copy function is used to copy matrix data.

MatrixLayer mlSrc = Project.ActiveLayer();  // Get the active matrix sheet
MatrixObject moSrc = mlSrc.MatrixObjects(0);  // Get the 1st matrix object in the sheet
MatrixLayer mlDst;
mlDst.Create("Origin");  // Create a new matrix sheet
MatrixObject moDst = mlDst.MatrixObjects(0);  // Get the 1st matrix object
bool bRet = matobj_copy(moDst, moSrc);  // Copy the active data to the newly created matrix

## Math on Matrix Data

To perform mathematical operation on matrix, it always gets the data out of matrix object into a data matrix, and then do the calculation, and put the data back into matrix object. The math includes multiplying matrix by constant, dot multiply, dot divide, dot power, cross, cumulative product, cumulative sum, difference, etc.

The following shows two examples on the matrix operations, one is multiply matrix by constant, and the other is dot multiply.

### Multiply Matrix by Constant

MatrixLayer ml = Project.ActiveLayer(); // Get active matrix sheet
MatrixObject mo = ml.MatrixObjects(0); // Get the first matrix object

//Get the reference of the internal data object of matrix window.
//Here assume data type of the matrix is double.
matrix<double>& mat = mo.GetDataObject();

// multiply 10 for each data in matrix, this change also effect on window
mat = mat * 10;

### Dot Multiply Two Matrix

// Attach to two matrix pages
MatrixPage matPage1("MBook1");
MatrixPage matPage2("MBook2");
if( !matPage1 || !matPage2 )
return;

// Get the matrix sheet from page by name or index
MatrixLayer matLayer1 = matPage1.Layers("MSheet1");
MatrixLayer matLayer2 = matPage2.Layers(1); // get the second sheet
if( !matLayer1 || !matLayer2 )
return;

// Get matrix object from matrix sheet by index, name is not allowed.
MatrixObject mo1 = matLayer1.MatrixObjects(0);
MatrixObject mo2 = matLayer2.MatrixObjects(0);

// Get the reference of the internal data object of matrix window
matrix<double>& mat1 = mo1.GetDataObject();
matrix<double>& mat2 = mo2.GetDataObject();

// Prepare new matrix window
MatrixPage matPageNew;
matPageNew.Create("Origin");
MatrixLayer mlNew = matPageNew.Layers(0);
MatrixObject moNew = mlNew.MatrixObjects(0);
matrix<double>& matNew = moNew.GetDataObject();

// Copy values from mat1 to new matrix
matNew = mat1;

// Multiply two matrices element by element and put result
// to a newly created matrix window
matNew.DotMultiply(mat2);

## Conversion between Matrix Object and Vector

The methods matrixbase::GetAsVector and matrixbase::SetByVector can be used to convert between matrix object and vector.

// To vector
MatrixLayer ml = Project.ActiveLayer();  // Active matrix sheet
MatrixObject mo = ml.MatrixObjects(0);  // The 1st matrix object
matrixbase &mb = mo.GetDataObject();  // Get data from matrix object
vector vb;
mb.GetAsVector(vb);  // Convert the matrix data into vector

// From vector
MatrixLayer ml1;
ml1.Create("Origin");  // Create a matrix sheet
MatrixObject mo1 = ml1.MatrixObjects(0);  // Get matrix object
matrixbase &mb1 = mo1.GetDataObject();  // Get data object
mb1.SetSize(2, 3);  // Set size 2 rows x 3 columns
vector v = {1, 2, 3, 4, 5, 6};  // Vector data
// Set vector data to matrix object
// First row: 1, 2, 3
// Second row: 4, 5, 6
int iRet = mb1.SetByVector(v);

## Manipulate Matrix Object with Complex Values

Origin C provides a set of methods in matrixbase class for handling complex, including making a complex matrix from two real matrices, getting real and imaginary, getting phase and amplitude, calculating conjugate, etc.

The following code is used to set a matrix object as complex matrix with two real matrices data, and then get its real, imaginary, phase, and amplitude into separate matrix objects, and then use the conjugate to replace the original complex matrix object.

void MatrixObject_Complex_EX()
{
// Original data for real
matrix mR =
{
{2, 2, 2, 0},
{0, 1, 99, 99}
};
// Original data for imaginary
matrix mI =
{
{3, -3, 0, 3},
{0, 99, 1, 99}
};
matrix<complex> mC;
// Create a complex data
int iRet = mC.MakeComplex(mR, mI);
if(iRet == 0)
{
// Create a new matrix sheet for complex data
MatrixLayer ml;
ml.Create("Origin");
MatrixObject mo = ml.MatrixObjects(0);
ml.SetInternalData(FSI_COMPLEX);
matrixbase &mb = mo.GetDataObject();
mb = mC;

// Get real part
matrix mReal;
mb.GetReal(mReal);
// Get imaginary part
matrix mImg;
mb.GetImaginary(mImg);
// Get phase
matrix mPha;
mb.GetPhase(mPha);
// Get amplitude
matrix mAmp;
mb.GetAmplitude(mAmp);
// Create new matrix sheet for the results
MatrixLayer mlRes;
mlRes.Create("Origin");
// Set 4 matrix objects, the same size as the matrix
mlRes.SetSize(4, mb.GetNumRows(), mb.GetNumCols());
MatrixObject moReal = mlRes.MatrixObjects(0);
MatrixObject moImg = mlRes.MatrixObjects(1);
MatrixObject moPha = mlRes.MatrixObjects(2);
MatrixObject moAmp = mlRes.MatrixObjects(3);
matrixbase &mbReal = moReal.GetDataObject();
matrixbase &mbImg = moImg.GetDataObject();
matrixbase &mbPha = moPha.GetDataObject();
matrixbase &mbAmp = moAmp.GetDataObject();
mbReal = mReal;  // Set real part to matrix object
mbImg = mImg;  // Set imaginary part to matrix object
mbPha = mPha;  // Set phase to matrix object
mbAmp = mAmp;  // Set amplitude to matrix object

// Use the conjugate to replace the original complex matrix
mb.Conjugate();
}
}

## Transform Matrix Object Data

Origin C contains a set of methods in matrixbase for the matrix transformation, such as flip a matrix horizontally or vertically, rotate a matrix, shrink a matrix, transpose a matrix, etc.

MatrixLayer ml = Project.ActiveLayer();
MatrixObject mo = ml.MatrixObjects(0);
matrixbase &mb = mo.GetDataObject();

mb.FlipHorizontal();  // Flip horizontally
mb.FlipVertical();  // Flip vertically
mb.Rotate(90);  // Rotate 90 degrees counter-clockwise, need to be multiple of 90
mb.Shrink(2, 2);  // Shrink by factor of 2 for both row and column
mb.Transpose();  // Transpose