.. 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 36-39 Perform required imports and start PyAnsys ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Perform required imports. .. GENERATED FROM PYTHON SOURCE LINES 39-48 .. 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() .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/pyansys-math/pyansys-math/.venv/lib/python3.10/site-packages/ansys/tools/common/cyberchannel.py:187: UserWarning: Starting gRPC client without TLS on 127.0.0.1:21000. This is INSECURE. Consider using a secure connection. warn(f"Starting gRPC client without TLS on {target}. This is INSECURE. Consider using a secure connection.") .. GENERATED FROM PYTHON SOURCE LINES 49-54 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 54-60 .. code-block:: Python v = mm.ones(2) w = mm.rand(2) print(v) print(w) .. rst-class:: sphx-glr-script-out .. code-block:: none VSGRCF : Size : 2 1.000e+00 1.000e+00 ERWNSA : Size : 2 4.170e-01 9.972e-01 .. GENERATED FROM PYTHON SOURCE LINES 61-64 Plot vectors ~~~~~~~~~~~~ Plot the created vectors. .. GENERATED FROM PYTHON SOURCE LINES 64-73 .. 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 74-86 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 86-91 .. code-block:: Python z = v + w z.norm() .. rst-class:: sphx-glr-script-out .. code-block:: none 2.448815734735383 .. GENERATED FROM PYTHON SOURCE LINES 92-103 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 103-107 .. 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 108-110 Equivalent operator for subtraction: ``z = v - w`` .. GENERATED FROM PYTHON SOURCE LINES 110-114 .. code-block:: Python z = mm.subtract(v, w) print(z) .. rst-class:: sphx-glr-script-out .. code-block:: none UZFSUF : Size : 2 5.830e-01 2.815e-03 .. GENERATED FROM PYTHON SOURCE LINES 115-117 Equivalent dot operation for the product of two vectors: .. GENERATED FROM PYTHON SOURCE LINES 117-122 .. 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 123-127 Perform in-place operations (without copying vectors) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Perform in-place addition. .. GENERATED FROM PYTHON SOURCE LINES 127-132 .. code-block:: Python v += v print(v) .. rst-class:: sphx-glr-script-out .. code-block:: none VSGRCF : Size : 2 2.000e+00 2.000e+00 .. GENERATED FROM PYTHON SOURCE LINES 133-134 Perform in-place multiplication. .. GENERATED FROM PYTHON SOURCE LINES 134-138 .. code-block:: Python v *= 2 print(v) .. rst-class:: sphx-glr-script-out .. code-block:: none VSGRCF : Size : 2 4.000e+00 4.000e+00 .. GENERATED FROM PYTHON SOURCE LINES 139-140 Perform another in-place multiplication. .. GENERATED FROM PYTHON SOURCE LINES 140-145 .. code-block:: Python v /= 2.0 print(v) .. rst-class:: sphx-glr-script-out .. code-block:: none VSGRCF : Size : 2 2.000e+00 2.000e+00 .. GENERATED FROM PYTHON SOURCE LINES 146-149 Working with dense matrices ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Allocate two dense matrices with random values. .. GENERATED FROM PYTHON SOURCE LINES 149-154 .. 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 155-156 Add these 2 dense matrices and then scale the result matrix. .. GENERATED FROM PYTHON SOURCE LINES 156-163 .. code-block:: Python m3 = m1 + m2 print(m3) m3 *= 2 print(m3) .. rst-class:: sphx-glr-script-out .. code-block:: none NIRMGL: [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 NIRMGL: [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 164-165 Transpose a matrix. .. GENERATED FROM PYTHON SOURCE LINES 165-170 .. code-block:: Python m4 = m3.T print(m4) .. rst-class:: sphx-glr-script-out .. code-block:: none CIYCHD: [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 171-172 As for vectors, methods are available as an alternative to operators. .. GENERATED FROM PYTHON SOURCE LINES 172-177 .. code-block:: Python m3 = mm.add(m1, m2) print(m3) .. rst-class:: sphx-glr-script-out .. code-block:: none RZLOZV: [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 178-179 Compute a matrix vector multiplication. .. GENERATED FROM PYTHON SOURCE LINES 179-183 .. code-block:: Python mw = m3.dot(m4) print(mw) .. rst-class:: sphx-glr-script-out .. code-block:: none DBNUSD: [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 184-189 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 189-194 .. code-block:: Python type(m1) print(m1) m1 .. rst-class:: sphx-glr-script-out .. code-block:: none YIHPEH: [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 195-197 Here is an example with an AnsMath vector. .. GENERATED FROM PYTHON SOURCE LINES 197-202 .. code-block:: Python type(w) print(w) w .. rst-class:: sphx-glr-script-out .. code-block:: none ERWNSA : Size : 2 4.170e-01 9.972e-01 AnsMath vector size 2 .. GENERATED FROM PYTHON SOURCE LINES 203-210 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 210-216 .. 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 217-221 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 221-225 .. code-block:: Python print(np.max(apdl_mat)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9990405155112967 .. GENERATED FROM PYTHON SOURCE LINES 226-229 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 229-234 .. 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 235-238 Stop PyAnsys Math ~~~~~~~~~~~~~~~~~ Stop PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 238-240 .. code-block:: Python mm._mapdl.exit() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.575 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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: basic_operations.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_