.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\00-basic\10-math_operations.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_00-basic_10-math_operations.py: .. _ref_math_operators_example: Mathematical Operations ~~~~~~~~~~~~~~~~~~~~~~~ DPF provides operators for implementing mathematical operations, ranging from addition and multiplication to FFT and QR solving. For a complete list, see :ref:`ref_dpf_operators_reference`, under the math section. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python # Import the necessary modules import ansys.dpf.core as dpf .. GENERATED FROM PYTHON SOURCE LINES 40-42 Addition ~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 42-55 .. code-block:: Python # Initialize Fields num_entities = 2 field1 = dpf.Field(nentities=2) field2 = dpf.Field(nentities=2) # By default, Fields contain 3d vectors. # So with three entities we need nine values. field1.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] field2.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] field1.scoping.ids = range(num_entities) field2.scoping.ids = range(num_entities) .. GENERATED FROM PYTHON SOURCE LINES 56-57 Once the fields are ready, we can instantiate an operator. .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: Python add_op = dpf.operators.math.add(field1, field2) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Finally, we use eval() to compute and retrieve the result. .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: Python field3 = add_op.eval() # = [[2. 4. 6.] [8. 10. 12.]] print(field3.data) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 2. 4. 6.] [ 8. 10. 12.]] .. GENERATED FROM PYTHON SOURCE LINES 67-69 Dot product ~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 69-77 .. code-block:: Python dot_op = dpf.operators.math.generalized_inner_product(field1, field2) # (1. * 1.) + (2. * 2.) + (3. * 3.) = 14. # (4. * 4.) + (5. * 5.) + (6. * 6.) = 77. field3 = dot_op.eval() print(field3.data) .. rst-class:: sphx-glr-script-out .. code-block:: none [14. 77.] .. GENERATED FROM PYTHON SOURCE LINES 78-80 Power ~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 80-91 .. code-block:: Python field = dpf.Field(nentities=1) field1.data = [1.0, 2.0, 3.0] field1.scoping.ids = [1] pow_op = dpf.operators.math.pow(field1, 3.0) # [1. 8. 27.] field3 = pow_op.eval() print(field3.data) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 1. 8. 27.]] .. GENERATED FROM PYTHON SOURCE LINES 92-94 L2 norm ~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 94-102 .. code-block:: Python field1.data = [16.0, -8.0, 2.0] norm_op = dpf.operators.math.norm(field1) # [ 18. ] field3 = norm_op.eval() print(field3.data) .. rst-class:: sphx-glr-script-out .. code-block:: none [18.] .. GENERATED FROM PYTHON SOURCE LINES 103-109 Accumulate ~~~~~~~~~~ First we define fields. By default, fields represent 3D vectors so one elementary data is a 3D vector. The optional weights field is a field which takes one value per entity, so we need to change its dimensionality (1D). .. GENERATED FROM PYTHON SOURCE LINES 109-117 .. code-block:: Python num_entities = 3 input_field = dpf.Field(nentities=num_entities) weights_field = dpf.Field(num_entities) weights_field.dimensionality = dpf.Dimensionality([1]) input_field.scoping.ids = range(num_entities) weights_field.scoping.ids = range(num_entities) .. GENERATED FROM PYTHON SOURCE LINES 118-120 Fill fields with data. Add nine values because there are three entities. .. GENERATED FROM PYTHON SOURCE LINES 120-121 .. code-block:: Python input_field.data = [-2.0, 2.0, 4.0, -5.0, 0.5, 1.0, 7.0, 3.0, -3.0] .. GENERATED FROM PYTHON SOURCE LINES 122-123 Three weights, one per entity. .. GENERATED FROM PYTHON SOURCE LINES 123-125 .. code-block:: Python weights_field.data = [0.5, 2.0, 0.5] .. GENERATED FROM PYTHON SOURCE LINES 126-127 Retrieve the result. .. GENERATED FROM PYTHON SOURCE LINES 127-135 .. code-block:: Python acc = dpf.operators.math.accumulate(fieldA=input_field, weights=weights_field) output_field = acc.outputs.field() # (-2.0 * 0.5) + (-5.0 * 2.0) + (7.0 * 0.5) = -7.5 # (2.0 * 0.5) + (0.5 * 2.0) + (3.0 * 0.5) = 3.5 # (4.0 * 0.5) + (1.0 * 2.0) + (-3.0 * 0.5) = 2.5 print(output_field.data) .. rst-class:: sphx-glr-script-out .. code-block:: none [[-7.5 3.5 2.5]] .. GENERATED FROM PYTHON SOURCE LINES 136-138 With scoping ~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 138-141 .. code-block:: Python field1.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] field2.data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] .. GENERATED FROM PYTHON SOURCE LINES 142-150 Next, we need to provide information about the scoping. DPF needs to know the IDs of the data we just provided, so that it can apply an operator on a subset of the original data. By providing these integers we only select the data with an ID in common. Here we are selecting the third elementary data of the first field, and the first elementary data of the second field, Other elementary data is not taken into account when using an operator that needs two operands. .. GENERATED FROM PYTHON SOURCE LINES 150-162 .. code-block:: Python field1.scoping.ids = [1, 2, 3] field2.scoping.ids = [3, 4, 5] add_op = dpf.operators.math.add(field1, field2) field3 = add_op.eval() # Only the third entity was changed # because it is the only operator where two operands were provided. print(field3.data) # [[8. 10. 12.]] print(field3.get_entity_data_by_id(3)) .. rst-class:: sphx-glr-script-out .. code-block:: none [[ 1. 2. 3.] [ 4. 5. 6.] [ 8. 10. 12.] [ 4. 5. 6.] [ 7. 8. 9.]] [[ 8. 10. 12.]] .. GENERATED FROM PYTHON SOURCE LINES 163-164 Dot product .. GENERATED FROM PYTHON SOURCE LINES 164-172 .. code-block:: Python dot_op = dpf.operators.math.generalized_inner_product(field1, field2) # We obtain zeros for IDs where there could not be two operands. # (7. * 1.) + (8. * 2.) + (9. * 3.) = 50. # [0. 0. 50. 0. 0.] field3 = dot_op.eval() print(field3.data) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 0. 0. 50. 0. 0.] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.492 seconds) .. _sphx_glr_download_examples_00-basic_10-math_operations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 10-math_operations.ipynb <10-math_operations.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 10-math_operations.py <10-math_operations.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 10-math_operations.zip <10-math_operations.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_