OperationConst(Op: MatrixOperation; Value: Variant);
Op. Mathematical operation.
Value. Matrix constant or iterator.
The OperationConst method executes mathematical operations between the current matrix and constant or matrix iterator.
Available values of the Value parameter:
constant. After executing the operation the constant value is applied to all matrix elements.
iterator. It is used to execute operation for the matrix elements with non-empty values. To set matrix iterator, use the IMatrix.CreateIterator property or the IMatrixQuery.Select method.
Executing the example requires that the repository contains MDM dictionaries with the DICT_1 and DICT_2 identifiers.
Add links to the Dimensions, MatFin, Matrix, and Metabase system assemblies.
Sub UserProc;
Var
MB: IMetabase;
Obj_1, Obj_2: IMetabaseObjectDescriptor;
Dim_1, Dim_2: IDimInstance;
Matrix_1, Matrix_2: IMatrix;
MatrixFact: IMatrixFactory;
Query: IMatrixQuery;
Range: IMatrixQueryFilterRange;
SelectionFact: IDimSelectionSetFactory;
Selection: IDimSelectionSet;
x, y: Integer;
Coord: IMatrixCoord;
Begin
// Create a collection of dictionary selections
SelectionFact := New DimSelectionSetFactory.Create;
Selection := SelectionFact.CreateDimSelectionSet;
// Add to selection all elements of dictionaries with specified identifiers
MB := MetabaseClass.Active;
Obj_1 := MB.ItemById("DICT_1");
Dim_1 := Obj_1.Open(Null) As IDimInstance;
Selection.Add(Dim_1).SelectAll;
Obj_2 := MB.ItemById("DICT_2");
Dim_2 := Obj_2.Open(Null) As IDimInstance;
Selection.Add(Dim_2).SelectAll;
// Create matrix structures
MatrixFact := New MatrixFactory.Create As IMatrixFactory;
// Create matrices based on the specified dimension selection
Matrix_1 := MatrixFact.CreateMatrix(Selection);
Matrix_2 := MatrixFact.CreateMatrix(Selection);
// Populate with random numbers and display source matrix in the console
Coord := Matrix_1.CreateCoord;
Debug.WriteLine("Source matrix:");
For x := 0 To 4 Do
For y := 0 To 4 Do
Coord.Item(0) := x;
Coord.Item(1) := y;
Matrix_1.Item(Coord) := Math.RandBetweenI(10, 20);
Debug.Write(Matrix_1.Item(Coord) + " ");
End For;
Debug.WriteLine("");
End For;
// Populate with random numbers and display matrix used in mathematical operation in the console
Coord := Matrix_2.CreateCoord;
Debug.WriteLine("Matrix used in mathematical operation:");
For x := 0 To 4 Do
For y := 0 To 4 Do
Coord.Item(0) := x;
Coord.Item(1) := y;
Matrix_2.Item(Coord) := Math.RandBetweenI(10, 20);
Debug.Write(Matrix_2.Item(Coord) + " ");
End For;
Debug.WriteLine("");
End For;
// Create a query to get information about specified matrix data
Query := MatrixFact.CreateQuery(Matrix_2);
// Set up matrix values filtering
Range := Query.Filter.Ranges.Add;
// Set minimum value of filtering range
Range.Min := 12;
// Set maximum value of filtering range
Range.Max := 15;
// Execute summation operation in the specified range
Matrix_1.OperationConst(MatrixOperation.Add, Query.Select_);
Debug.WriteLine("Result of summing all numbers from 12 to 15 from the second matrix with corresponding elements from the first one:");
// Display output matrix to the console
Coord := Matrix_1.CreateCoord;
For x := 0 To 4 Do
For y := 0 To 4 Do
Coord.Item(0) := x;
Coord.Item(1) := y;
Debug.Write(Matrix_1.Item(Coord) + " ");
End For;
Debug.WriteLine("");
End For;
End Sub UserProc;
After executing the example the matrices with random values are created. All matrix values in the range from 12 to 15 from the second matrix and corresponding elements from the first matrix will be summed. All matrices will be displayed in the console, for example:
Source matrix:
12 17 17 11 14
18 14 10 11 18
17 14 12 19 12
11 18 19 19 16
14 16 17 12 10
Matrix used in mathematical operation:
16 14 17 13 16
11 13 14 16 11
18 11 19 16 16
15 17 20 18 18
19 13 15 16 16
Result of summing all numbers from 12 to 15 from the second matrix with corresponding elements from the first one:
12 31 17 24 14
18 27 24 11 18
17 14 12 19 12
26 18 19 19 16
14 29 32 12 10
See also: