.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/basic_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_basic_operations.py: .. _ref_pyansys-math_basic: PyAnsys Math basic operations ----------------------------- This tutorial shows how you can use PyAnsys Math for basic operations on AnsMath vectors and matrices in the APDL memory workspace. .. GENERATED FROM PYTHON SOURCE LINES 35-38 Perform required imports and start PyAnsys ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Perform required imports. .. GENERATED FROM PYTHON SOURCE LINES 38-47 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import ansys.math.core.math as pymath # Start PyAnsys Math as a service. mm = pymath.AnsMath() .. GENERATED FROM PYTHON SOURCE LINES 48-53 Create and manipulate vectors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Create two AnsMath vectors of size 5. The :math:`\vec{v}` method is initialized with ones, and the :math:`\vec{w}` is filled with random values. .. GENERATED FROM PYTHON SOURCE LINES 53-59 .. code-block:: Python v = mm.ones(2) w = mm.rand(2) print(v) print(w) .. rst-class:: sphx-glr-script-out .. code-block:: none GXLNSN : Size : 2 1.000e+00 1.000e+00 LTZGZN : Size : 2 4.170e-01 9.972e-01 .. GENERATED FROM PYTHON SOURCE LINES 60-63 Plot vectors ~~~~~~~~~~~~ Plot the created vectors. .. GENERATED FROM PYTHON SOURCE LINES 63-72 .. code-block:: Python origin = np.array([[0, 0], [0, 0]]) plt.title("Vectors V and W") plt.quiver(*origin, v, w, angles="xy", scale_units="xy", scale=1, color=["orange", "gray"]) plt.xlim(-1.5, 1.5) plt.ylim(-1.5, 1.5) plt.show() .. image-sg:: /examples/images/sphx_glr_basic_operations_001.png :alt: Vectors V and W :srcset: /examples/images/sphx_glr_basic_operations_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 73-85 Use operators on vectors ~~~~~~~~~~~~~~~~~~~~~~~~ Like NumPy vectors, AnsMath vectors can use most of the standard operators, such as ``+``, ``-``, ``+=``, ``-=``, and ``*=``. Here this form is used: :math:`\vec{z}=\vec{v}+\vec{w}` Compute :math:`\|z\|_2`. (The default norm is nrm2. Note that you can use ``.norm('nrm1')`` or ``.norm('nrminf')`` for different normals. For more information, see `help(z.norm)`. .. GENERATED FROM PYTHON SOURCE LINES 85-90 .. code-block:: Python z = v + w z.norm() .. rst-class:: sphx-glr-script-out .. code-block:: none 2.448815734735383 .. GENERATED FROM PYTHON SOURCE LINES 91-102 Methods ~~~~~~~ Alternatively you can use methods, following the NumPy standards. Available methods are: - ``mm.add()`` - ``mm.subtract()`` - ``mm.dot()`` Equivalent operator for addition: ``z = v + w`` .. GENERATED FROM PYTHON SOURCE LINES 102-106 .. code-block:: Python z = mm.add(v, w) z.norm() .. rst-class:: sphx-glr-script-out .. code-block:: none 2.448815734735383 .. GENERATED FROM PYTHON SOURCE LINES 107-109 Equivalent operator for subtraction: ``z = v - w`` .. GENERATED FROM PYTHON SOURCE LINES 109-113 .. code-block:: Python z = mm.subtract(v, w) print(z) .. rst-class:: sphx-glr-script-out .. code-block:: none YKBNJO : Size : 2 5.830e-01 2.815e-03 .. GENERATED FROM PYTHON SOURCE LINES 114-116 Equivalent dot operation for the product of two vectors: .. GENERATED FROM PYTHON SOURCE LINES 116-121 .. code-block:: Python vw = mm.dot(v, w) print("Dot product :", str(vw)) .. rst-class:: sphx-glr-script-out .. code-block:: none Dot product : 1.4142068068031222 .. GENERATED FROM PYTHON SOURCE LINES 122-126 Perform in-place operations (without copying vectors) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Perform in-place addition. .. GENERATED FROM PYTHON SOURCE LINES 126-131 .. code-block:: Python v += v print(v) .. rst-class:: sphx-glr-script-out .. code-block:: none GXLNSN : Size : 2 2.000e+00 2.000e+00 .. GENERATED FROM PYTHON SOURCE LINES 132-133 Perform in-place multiplication. .. GENERATED FROM PYTHON SOURCE LINES 133-137 .. code-block:: Python v *= 2 print(v) .. rst-class:: sphx-glr-script-out .. code-block:: none GXLNSN : Size : 2 4.000e+00 4.000e+00 .. GENERATED FROM PYTHON SOURCE LINES 138-139 Perform another in-place multiplication. .. GENERATED FROM PYTHON SOURCE LINES 139-144 .. code-block:: Python v /= 2.0 print(v) .. rst-class:: sphx-glr-script-out .. code-block:: none GXLNSN : Size : 2 2.000e+00 2.000e+00 .. GENERATED FROM PYTHON SOURCE LINES 145-148 Working with dense matrices ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Allocate two dense matrices with random values. .. GENERATED FROM PYTHON SOURCE LINES 148-153 .. code-block:: Python m1 = mm.rand(4, 5) m2 = mm.ones(4, 5) m1, m2 .. rst-class:: sphx-glr-script-out .. code-block:: none (AnsMath dense matrix (4, 5, AnsMath dense matrix (4, 5) .. GENERATED FROM PYTHON SOURCE LINES 154-155 Add these 2 dense matrices and then scale the result matrix. .. GENERATED FROM PYTHON SOURCE LINES 155-162 .. code-block:: Python m3 = m1 + m2 print(m3) m3 *= 2 print(m3) .. rst-class:: sphx-glr-script-out .. code-block:: none LXUZRW: [1,1]: 1.417e+00 [1,2]: 1.000e+00 [1,3]: 1.147e+00 [1,4]: 1.186e+00 [1,5]: 1.397e+00 [2,1]: 1.997e+00 [2,2]: 1.128e+00 [2,3]: 1.236e+00 [2,4]: 1.388e+00 [2,5]: 1.936e+00 [3,1]: 1.720e+00 [3,2]: 1.302e+00 [3,3]: 1.092e+00 [3,4]: 1.346e+00 [3,5]: 1.539e+00 [4,1]: 1.933e+00 [4,2]: 1.999e+00 [4,3]: 1.397e+00 [4,4]: 1.670e+00 [4,5]: 1.846e+00 LXUZRW: [1,1]: 2.834e+00 [1,2]: 2.000e+00 [1,3]: 2.294e+00 [1,4]: 2.373e+00 [1,5]: 2.794e+00 [2,1]: 3.994e+00 [2,2]: 2.256e+00 [2,3]: 2.472e+00 [2,4]: 2.776e+00 [2,5]: 3.871e+00 [3,1]: 3.441e+00 [3,2]: 2.605e+00 [3,3]: 2.185e+00 [3,4]: 2.691e+00 [3,5]: 3.078e+00 [4,1]: 3.865e+00 [4,2]: 3.998e+00 [4,3]: 2.793e+00 [4,4]: 3.339e+00 [4,5]: 3.693e+00 .. GENERATED FROM PYTHON SOURCE LINES 163-164 Transpose a matrix. .. GENERATED FROM PYTHON SOURCE LINES 164-169 .. code-block:: Python m4 = m3.T print(m4) .. rst-class:: sphx-glr-script-out .. code-block:: none XHVIOQ: [1,1]: 2.834e+00 [1,2]: 3.994e+00 [1,3]: 3.441e+00 [1,4]: 3.865e+00 [2,1]: 2.000e+00 [2,2]: 2.256e+00 [2,3]: 2.605e+00 [2,4]: 3.998e+00 [3,1]: 2.294e+00 [3,2]: 2.472e+00 [3,3]: 2.185e+00 [3,4]: 2.793e+00 [4,1]: 2.373e+00 [4,2]: 2.776e+00 [4,3]: 2.691e+00 [4,4]: 3.339e+00 [5,1]: 2.794e+00 [5,2]: 3.871e+00 [5,3]: 3.078e+00 [5,4]: 3.693e+00 .. GENERATED FROM PYTHON SOURCE LINES 170-171 As for vectors, methods are available as an alternative to operators. .. GENERATED FROM PYTHON SOURCE LINES 171-176 .. code-block:: Python m3 = mm.add(m1, m2) print(m3) .. rst-class:: sphx-glr-script-out .. code-block:: none AFNRNW: [1,1]: 1.417e+00 [1,2]: 1.000e+00 [1,3]: 1.147e+00 [1,4]: 1.186e+00 [1,5]: 1.397e+00 [2,1]: 1.997e+00 [2,2]: 1.128e+00 [2,3]: 1.236e+00 [2,4]: 1.388e+00 [2,5]: 1.936e+00 [3,1]: 1.720e+00 [3,2]: 1.302e+00 [3,3]: 1.092e+00 [3,4]: 1.346e+00 [3,5]: 1.539e+00 [4,1]: 1.933e+00 [4,2]: 1.999e+00 [4,3]: 1.397e+00 [4,4]: 1.670e+00 [4,5]: 1.846e+00 .. GENERATED FROM PYTHON SOURCE LINES 177-178 Compute a matrix vector multiplication. .. GENERATED FROM PYTHON SOURCE LINES 178-182 .. code-block:: Python mw = m3.dot(m4) print(mw) .. rst-class:: sphx-glr-script-out .. code-block:: none JUPONQ: [1,1]: 1.536e+01 [1,2]: 1.945e+01 [1,3]: 1.748e+01 [1,4]: 2.180e+01 [2,1]: 1.945e+01 [2,2]: 2.492e+01 [2,3]: 2.220e+01 [2,4]: 2.746e+01 [3,1]: 1.748e+01 [3,2]: 2.220e+01 [3,3]: 2.005e+01 [3,4]: 2.508e+01 [4,1]: 2.180e+01 [4,2]: 2.746e+01 [4,3]: 2.508e+01 [4,4]: 3.176e+01 .. GENERATED FROM PYTHON SOURCE LINES 183-188 AnsMath matrices can be identified by printing, viewing their types, or using the ``__repr__`` method by simply typing out the variable. Here is an example with an AnsMath matrix. .. GENERATED FROM PYTHON SOURCE LINES 188-193 .. code-block:: Python type(m1) print(m1) m1 .. rst-class:: sphx-glr-script-out .. code-block:: none NAZDQO: [1,1]: 4.170e-01 [1,2]: 1.144e-04 [1,3]: 1.468e-01 [1,4]: 1.863e-01 [1,5]: 3.968e-01 [2,1]: 9.972e-01 [2,2]: 1.281e-01 [2,3]: 2.361e-01 [2,4]: 3.879e-01 [2,5]: 9.355e-01 [3,1]: 7.203e-01 [3,2]: 3.023e-01 [3,3]: 9.234e-02 [3,4]: 3.456e-01 [3,5]: 5.388e-01 [4,1]: 9.326e-01 [4,2]: 9.990e-01 [4,3]: 3.966e-01 [4,4]: 6.697e-01 [4,5]: 8.463e-01 AnsMath dense matrix (4, 5 .. GENERATED FROM PYTHON SOURCE LINES 194-196 Here is an example with an AnsMath vector. .. GENERATED FROM PYTHON SOURCE LINES 196-201 .. code-block:: Python type(w) print(w) w .. rst-class:: sphx-glr-script-out .. code-block:: none LTZGZN : Size : 2 4.170e-01 9.972e-01 AnsMath vector size 2 .. GENERATED FROM PYTHON SOURCE LINES 202-209 Use NumPy methods on AnsMath objects ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Regardless of the underlying AnsMath object type, you are generally able to perform most NumPy or SciPy operations on these arrays. You can do this in one of two ways. You can convert a matrix to a NumPy array. .. GENERATED FROM PYTHON SOURCE LINES 209-215 .. code-block:: Python apdl_mat = mm.rand(5, 5) np_mat = apdl_mat.asarray() print(np_mat) .. rst-class:: sphx-glr-script-out .. code-block:: none [[4.17021999e-01 1.28124448e-01 9.23385957e-02 6.69746040e-01 4.19194519e-01] [9.97184808e-01 3.02332568e-01 3.96580726e-01 3.96767469e-01 3.13273513e-01] [7.20324489e-01 9.99040516e-01 1.86260211e-01 9.35539073e-01 6.85219501e-01] [9.32557361e-01 1.46755893e-01 3.87910740e-01 5.38816732e-01 5.24548163e-01] [1.14381197e-04 2.36088976e-01 3.45560725e-01 8.46310918e-01 2.04452249e-01]] .. GENERATED FROM PYTHON SOURCE LINES 216-220 Alternatively, you can use NumPy to compute the maximum of the array. This works because PyAnsys Math copies over the matrix to the local Python memory and then computes the maximum using NumPy. .. GENERATED FROM PYTHON SOURCE LINES 220-224 .. code-block:: Python print(np.max(apdl_mat)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9990405155112967 .. GENERATED FROM PYTHON SOURCE LINES 225-228 While this works for most NumPy operations, keep in mind that operations supported within PyAnsys Math (such as adding or multiplying arrays) compute much faster because the data is not copied. .. GENERATED FROM PYTHON SOURCE LINES 228-233 .. code-block:: Python apdl_arr = mm.rand(5, 5) np_array = apdl_mat.asarray() print(np.allclose(apdl_mat, np_array)) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 234-237 Stop PyAnsys Math ~~~~~~~~~~~~~~~~~ Stop PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 237-239 .. code-block:: Python mm._mapdl.exit() .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/ansys/mapdl/core/launcher.py:811: UserWarning: The environment variable 'PYMAPDL_START_INSTANCE' is set, hence the argument 'start_instance' is overwritten. warnings.warn( .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.544 seconds) .. _sphx_glr_download_examples_basic_operations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: basic_operations.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: basic_operations.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_