\section ExamplesArrays Arrays \subsection ExamplesArraysCreate Create \anchor BEGIN_PYTHON_ONLY \subsubsection MEDCouplingArraySteps0 Building an array from scratch in Python \anchor MEDCouplingArraySteps0Double
Info of components : "a2" "a1" "v3" "v4" Tuple #0 : 2 1 0 0 Tuple #1 : 4 3 0 0 Tuple #2 : 6 5 0 0 Tuple #3 : 0 0 0 0The same result can be achieved other way (except that component info is not copied): \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setSelectedComponents3 \anchor py_mcdataarraydouble_setpartofvalues1
Info of components :"v1" "v2" "v3" "v4" Tuple #0 : 0 0 0 0 Tuple #1 : 0 7 8 0 Tuple #2 : 0 9 10 0 Tuple #3 : 0 0 0 0Here we re-fill \b da with zeros and copy \b dv into a component of \b da. Note that the last parameter \b strictCompoCompare should be \c False in this case, else \ref ParaMEDMEM::DataArrayDouble::setPartOfValues1() throws an exception because \b da has 2 components but only one target component is specified. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValues1_3
Tuple #0 : 0 7 0 0 Tuple #1 : 0 8 0 0 Tuple #2 : 0 9 0 0 Tuple #3 : 0 10 0 0Below more two variants of location of target values are shown. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValues1_4
Tuple #0 : 0 0 0 0 Tuple #1 : 7 8 9 10 Tuple #2 : 0 0 0 0 Tuple #3 : 0 0 0 0\snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValues1_5
Tuple #0 : 0 7 0 8 Tuple #1 : 0 0 0 0 Tuple #2 : 0 9 0 10 Tuple #3 : 0 0 0 0The same result can be achieved other way: \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValues1_6 \anchor py_mcdataarraydouble_setpartofvaluessimple1
Tuple #0 : 0 0 0 0 Tuple #1 : 0 7 7 0 Tuple #2 : 0 7 7 0 Tuple #3 : 0 0 0 0Here we re-fill \b da with zeros and assign \b dv to a component of \b da. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple1_3
Tuple #0 : 0 7 0 0 Tuple #1 : 0 7 0 0 Tuple #2 : 0 7 0 0 Tuple #3 : 0 7 0 0Below more two variants of location of target values are shown. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple1_4
Tuple #0 : 0 0 0 0 Tuple #1 : 7 7 7 7 Tuple #2 : 0 0 0 0 Tuple #3 : 0 0 0 0\snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple1_5
Tuple #0 : 0 7 0 7 Tuple #1 : 0 0 0 0 Tuple #2 : 0 7 0 7 Tuple #3 : 0 0 0 0The same result can be achieved other way: \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple1_6 \anchor py_mcdataarraydouble_setpartofvaluessimple2
Tuple #0 : 0 0 0 0 Tuple #1 : 0 7 7 0 Tuple #2 : 0 7 7 0 Tuple #3 : 0 0 0 0Here we re-fill \b da with zeros and assign \b dv to a component of \b da. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple2_3
Tuple #0 : 0 7 0 0 Tuple #1 : 0 7 0 0 Tuple #2 : 0 7 0 0 Tuple #3 : 0 7 0 0Below more two variants of location of target values are shown. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple2_4
Tuple #0 : 0 0 0 0 Tuple #1 : 7 7 7 7 Tuple #2 : 0 0 0 0 Tuple #3 : 0 0 0 0\snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple2_5
Tuple #0 : 0 7 0 7 Tuple #1 : 0 0 0 0 Tuple #2 : 0 7 0 7 Tuple #3 : 0 0 0 0\note \ref ParaMEDMEM::DataArrayDouble::setPartOfValuesSimple2() can't be explicitly called in Python. \anchor py_mcdataarraydouble_setpartofvaluessimple3
Tuple #0 : 0 0 0 0 Tuple #1 : 0 7 7 0 Tuple #2 : 0 7 7 0 Tuple #3 : 0 0 0 0Here we re-fill \b da with zeros and assign \b dv to a component of \b da. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple3_3
Tuple #0 : 0 7 0 0 Tuple #1 : 0 7 0 0 Tuple #2 : 0 7 0 0 Tuple #3 : 0 7 0 0Below more two variants of location of target values are shown. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple3_4
Tuple #0 : 0 0 0 0 Tuple #1 : 7 7 7 7 Tuple #2 : 0 0 0 0 Tuple #3 : 0 0 0 0\snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValuesSimple3_5
Tuple #0 : 0 7 0 7 Tuple #1 : 0 0 0 0 Tuple #2 : 0 7 0 7 Tuple #3 : 0 0 0 0\note \ref ParaMEDMEM::DataArrayDouble::setPartOfValuesSimple3() can't be explicitly called in Python. \anchor py_mcdataarraydouble_setpartofvalues2
Tuple #0 : 0 7 0 8 0 0 0 Tuple #1 : 0 9 0 10 0 0 0 Tuple #2 : 0 11 0 12 0 0 0 Tuple #3 : 0 0 0 0 0 0 0Every value of \b dv has been assigned to its own location within \b da. Now we re-fill \b da with zeros and rearrange \b dv to have 6 components. And we assign \b dv to the tuples of \b da with indices [0,2,3] . This is the second mode of usage. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValues2_3 The contents of \b dv have been assigned to each of specified tuples of \b da. Every value of \b dv is repeated in the 3 specified tuples within \b da.
Tuple #0 : 7 0 8 9 10 11 12 Tuple #1 : 0 0 0 0 0 0 0 Tuple #2 : 7 0 8 9 10 11 12 Tuple #3 : 7 0 8 9 10 11 12\note \ref ParaMEDMEM::DataArrayDouble::setPartOfValues2() can't be explicitly called in Python. \anchor py_mcdataarraydouble_setpartofvalues3
Tuple #0 : 0 7 0 8 0 0 0 Tuple #1 : 0 9 0 10 0 0 0 Tuple #2 : 0 11 0 12 0 0 0 Tuple #3 : 0 0 0 0 0 0 0Every value of \b dv has been assigned to its own location within \b da. Now we re-fill \b da with zeros and rearrange \b dv to have 6 components. And we assign \b dv to the tuples of \b da with indices [0,2] using \a slice notation "0:4:2". This is the second mode of usage. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_setPartOfValues3_3 The contents of \b dv have been assigned to each of specified tuples of \b da. Every value of \b dv is repeated in the 3 specified tuples within \b da.
Tuple #0 : 7 0 8 9 10 11 12 Tuple #1 : 0 0 0 0 0 0 0 Tuple #2 : 7 0 8 9 10 11 12 Tuple #3 : 0 0 0 0 0 0 0\note \ref ParaMEDMEM::DataArrayDouble::setPartOfValues3() can't be explicitly called in Python. \subsubsection py_mcdataarrayint_setpartofvalues Set part of values of DataArrayInt \anchor py_mcdataarrayint_setselectedcomponents
Info of components : "a2" "a1" "v3" "v4" Tuple #0 : 2 1 0 0 Tuple #1 : 4 3 0 0 Tuple #2 : 6 5 0 0 Tuple #3 : 0 0 0 0The same result can be achieved other way (except that component info is not copied): \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setSelectedComponents3 \anchor py_mcdataarrayint_setpartofvalues1
Info of components :"v1" "v2" "v3" "v4" Tuple #0 : 0 0 0 0 Tuple #1 : 0 7 8 0 Tuple #2 : 0 9 10 0 Tuple #3 : 0 0 0 0Here we re-fill \b da with zeros and copy \b dv into a component of \b da. Note that the last parameter \b strictCompoCompare should be \c False in this case, else \ref ParaMEDMEM::DataArrayInt::setPartOfValues1() throws an exception because \b da has 2 components but only one target component is specified. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValues1_3
Tuple #0 : 0 7 0 0 Tuple #1 : 0 8 0 0 Tuple #2 : 0 9 0 0 Tuple #3 : 0 10 0 0Below more two variants of location of target values are shown. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValues1_4
Tuple #0 : 0 0 0 0 Tuple #1 : 7 8 9 10 Tuple #2 : 0 0 0 0 Tuple #3 : 0 0 0 0\snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValues1_5
Tuple #0 : 0 7 0 8 Tuple #1 : 0 0 0 0 Tuple #2 : 0 9 0 10 Tuple #3 : 0 0 0 0The same result can be achieved other way: \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValues1_6 \anchor py_mcdataarrayint_setpartofvaluessimple1
Tuple #0 : 0 0 0 0 Tuple #1 : 0 7 7 0 Tuple #2 : 0 7 7 0 Tuple #3 : 0 0 0 0Here we re-fill \b da with zeros and assign \b dv to a component of \b da. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple1_3
Tuple #0 : 0 7 0 0 Tuple #1 : 0 7 0 0 Tuple #2 : 0 7 0 0 Tuple #3 : 0 7 0 0Below more two variants of location of target values are shown. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple1_4
Tuple #0 : 0 0 0 0 Tuple #1 : 7 7 7 7 Tuple #2 : 0 0 0 0 Tuple #3 : 0 0 0 0\snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple1_5
Tuple #0 : 0 7 0 7 Tuple #1 : 0 0 0 0 Tuple #2 : 0 7 0 7 Tuple #3 : 0 0 0 0The same result can be achieved other way: \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple1_6 \anchor py_mcdataarrayint_setpartofvaluessimple2
Tuple #0 : 0 0 0 0 Tuple #1 : 0 7 7 0 Tuple #2 : 0 7 7 0 Tuple #3 : 0 0 0 0Here we re-fill \b da with zeros and assign \b dv to a component of \b da. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple2_3
Tuple #0 : 0 7 0 0 Tuple #1 : 0 7 0 0 Tuple #2 : 0 7 0 0 Tuple #3 : 0 7 0 0Below more two variants of location of target values are shown. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple2_4
Tuple #0 : 0 0 0 0 Tuple #1 : 7 7 7 7 Tuple #2 : 0 0 0 0 Tuple #3 : 0 0 0 0\snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple2_5
Tuple #0 : 0 7 0 7 Tuple #1 : 0 0 0 0 Tuple #2 : 0 7 0 7 Tuple #3 : 0 0 0 0\note \ref ParaMEDMEM::DataArrayInt::setPartOfValuesSimple2() can't be explicitly called in Python. \anchor py_mcdataarrayint_setpartofvaluessimple3
Tuple #0 : 0 0 0 0 Tuple #1 : 0 7 7 0 Tuple #2 : 0 7 7 0 Tuple #3 : 0 0 0 0Here we re-fill \b da with zeros and assign \b dv to a component of \b da. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple3_3
Tuple #0 : 0 7 0 0 Tuple #1 : 0 7 0 0 Tuple #2 : 0 7 0 0 Tuple #3 : 0 7 0 0Below more two variants of location of target values are shown. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple3_4
Tuple #0 : 0 0 0 0 Tuple #1 : 7 7 7 7 Tuple #2 : 0 0 0 0 Tuple #3 : 0 0 0 0\snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValuesSimple3_5
Tuple #0 : 0 7 0 7 Tuple #1 : 0 0 0 0 Tuple #2 : 0 7 0 7 Tuple #3 : 0 0 0 0\note \ref ParaMEDMEM::DataArrayInt::setPartOfValuesSimple3() can't be explicitly called in Python. \anchor py_mcdataarrayint_setpartofvalues2
Tuple #0 : 0 7 0 8 0 0 0 Tuple #1 : 0 9 0 10 0 0 0 Tuple #2 : 0 11 0 12 0 0 0 Tuple #3 : 0 0 0 0 0 0 0Every value of \b dv has been assigned to its own location within \b da. Now we re-fill \b da with zeros and rearrange \b dv to have 6 components. And we assign \b dv to the tuples of \b da with indices [0,2,3] . This is the second mode of usage. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValues2_3 The contents of \b dv have been assigned to each of specified tuples of \b da. Every value of \b dv is repeated in the 3 specified tuples within \b da.
Tuple #0 : 7 0 8 9 10 11 12 Tuple #1 : 0 0 0 0 0 0 0 Tuple #2 : 7 0 8 9 10 11 12 Tuple #3 : 7 0 8 9 10 11 12\note \ref ParaMEDMEM::DataArrayInt::setPartOfValues2() can't be explicitly called in Python. \anchor py_mcdataarrayint_setpartofvalues3
Tuple #0 : 0 7 0 8 0 0 0 Tuple #1 : 0 9 0 10 0 0 0 Tuple #2 : 0 11 0 12 0 0 0 Tuple #3 : 0 0 0 0 0 0 0Every value of \b dv has been assigned to its own location within \b da. Now we re-fill \b da with zeros and rearrange \b dv to have 6 components. And we assign \b dv to the tuples of \b da with indices [0,2] using \a slice notation "0:4:2". This is the second mode of usage. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_setPartOfValues3_3 The contents of \b dv have been assigned to each of specified tuples of \b da. Every value of \b dv is repeated in the 3 specified tuples within \b da.
Tuple #0 : 7 0 8 9 10 11 12 Tuple #1 : 0 0 0 0 0 0 0 Tuple #2 : 7 0 8 9 10 11 12 Tuple #3 : 0 0 0 0 0 0 0\note \ref ParaMEDMEM::DataArrayInt::setPartOfValues3() can't be explicitly called in Python. \subsubsection py_mcdataarraydouble_getdifferentvalues Excluding coincident tuples from DataArrayDouble The code below creates an array of real values and than an array of unique values, not closer one to another than 0.2, is retrieved from it. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayDouble_getDifferentValues1 \subsubsection cpp_mcdataarraydouble_meldwith Concatenating DataArrayDouble's by appending components In this example we create two data arrays including \b same number of tuples and then we concatenate them using \ref ParaMEDMEM::DataArrayDouble::meldWith "meldWith()". \snippet MEDCouplingExamplesTest.cxx CppSnippet_DataArrayDouble_Meld1_1 Now the array \b da1 includes 7 tuples (as before) of 3 components each. Its components are: "c0da1","c1da1","c0da2". \subsubsection cpp_mcdataarrayint_meldwith Concatenating DataArrayInt's by appending components In this example we create two data arrays including \b same number of tuples and then we concatenate them using \ref ParaMEDMEM::DataArrayInt::meldWith "meldWith()". \snippet MEDCouplingExamplesTest.cxx CppSnippet_DataArrayInt_Meld1_1 Now the array \b da1 includes 7 tuples (as before) of 3 components each. Its components are: "c0da1","c1da1","c0da2". \subsection ExamplesArraysAccess Access \subsubsection cpp_mcdataarrayint_getTuple Getting a tuple of DataArrayInt In this simple example we create an array of integers arranged into 3 tuples per 2 components, and finally print the second tuple. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_getTuple_1 The output is
[9, 10]Note that we can traverse all tuples in the array by simply iterating over it as the code below does. \snippet MEDCouplingExamplesTest.py Snippet_DataArrayInt_getTuple_2 Its output follows.
(7, 8) (9, 10) (11, 12)\subsubsection cpp_mcdataarraydouble_getidsinrange Finding values in range in DataArrayDouble In this example we create an array \b da containing same values as ones returned by \c range( \c 10 ). Then we get an array of indices of values of \b da being in range [ 2.5, 6 ]. \snippet MEDCouplingExamplesTest.cxx CppSnippet_DataArrayDouble_getIdsInRange_1 As result contents of the array \b da2 are as follows.
Tuple #0 : 3 Tuple #1 : 4 Tuple #2 : 5 Tuple #3 : 6\subsubsection cpp_mcdataarraydouble_findcommontuples Finding coincident tuples in DataArrayDouble Let's create an array of 6 tuples and 2 components that can be considered as coordinates of 6 points in 2D space. \snippet MEDCouplingExamplesTest.cxx CppSnippet_DataArrayDouble_findCommonTuples1 Now we find points that are not far each from other than 1e-1. \snippet MEDCouplingExamplesTest.cxx CppSnippet_DataArrayDouble_findCommonTuples2 As we can realize from the above code, a hardcoded array \b expected3 is equal to the raw data of a DataArrayInt \b c and a hardcoded array \b expected4 is equal to the raw data of the DataArrayInt \b cI. The array \b c contains indices of 5 coincident points. The array \b cI shows us boundaries of (cI->getNumberOfTuples()-1) = 2 groups of coincident points: - The first group starts at index 0 and includes (3 - 0) = 3 points: 0,3,4. - The second group starts at index 3 and includes (5 - 3) = 2 points: 1,2. \subsubsection py_mcdataarraydouble_KeepSelectedComponents Creation of a sub-part of the DataArrayDouble by selecting components \snippet MEDCouplingExamplesTest.py SnippeDataArrayDoubleKeepSelectedComponents1_1 We created an array \b a1 containing 5 tuples of 4 components each (20 values). Now we are going to create an array \b a2 containing some components of \b a1. \snippet MEDCouplingExamplesTest.py SnippeDataArrayDoubleKeepSelectedComponents1_2 Now each tuple of \b a2 includes components named "b","c","b","c","a","a". Thus the result array \b a2 includes 30 elements (5 tuples per 6 components). \subsubsection py_mcdataarrayint_keepselectedcomponents Creation of a sub-part of the DataArrayInt by selecting components \snippet MEDCouplingExamplesTest.py SnippeDataArrayIntKeepSelectedComponents1_1 We created an array \b a1 containing 5 tuples of 4 components each (20 values). Now we are going to create an array \b a2 containing some components of \b a1. \snippet MEDCouplingExamplesTest.py SnippeDataArrayIntKeepSelectedComponents1_2 Now each tuple of \b a2 includes components named "b","c","b","c","a","a". Thus the result array \b a2 includes 30 elements (5 tuples per 6 components). Note that \ref ParaMEDMEM::DataArrayInt::keepSelectedComponents() "DataArrayInt::keepSelectedComponents()" is called, providing the same result, by the following python code: \snippet MEDCouplingExamplesTest.py SnippeDataArrayIntKeepSelectedComponents1_3