Array Math

This example will demonstrate how to perform a mathematical operation between two input arrays for any given source.

This filter allows the user to select two input data arrays on which to perform math operations. The input arrays are used in their order of selection for the operations.

This example demos: PVGeo.filters.ArrayMath

import numpy as np
import pyvista

from PVGeo.filters import ArrayMath

Create some input data. This can be any vtkDataObject

inp = pyvista.UniformGrid((10, 10, 4))
# Populate the tables
n = 400
arr0 = np.random.random(n)
arr1 = np.random.random(n)
inp["Array 0"] = arr0
inp["Array 1"] = arr1
/opt/hostedtoolcache/Python/3.8.13/x64/lib/python3.8/site-packages/pyvista/core/grid.py:469: PyvistaDeprecationWarning: Behavior of pyvista.UniformGrid has changed. First argument must be either a ``vtk.vtkImageData`` or path.
  warnings.warn(

Use the filter:

f = ArrayMath(operation="add", new_name="foo")
# Now get the result
output = f.apply(inp, "Array 0", "Array 1")
output
HeaderData Arrays
UniformGridInformation
N Cells243
N Points400
X Bounds0.000e+00, 9.000e+00
Y Bounds0.000e+00, 9.000e+00
Z Bounds0.000e+00, 3.000e+00
Dimensions10, 10, 4
Spacing1.000e+00, 1.000e+00, 1.000e+00
N Arrays3
NameFieldTypeN CompMinMax
Array 0Pointsfloat6411.013e-029.956e-01
Array 1Pointsfloat6415.150e-039.979e-01
fooPointsfloat6419.053e-021.954e+00


Note that the output now has three arrays

arr = output["foo"]
assert np.allclose(arr, arr0 + arr1)

Use a custom math operation:

def power(arr0, arr1):
    return arr0**arr1


# Use filter generated above
f.set_operation(power)
f.set_new_array_name("powered")
f.update()

# Now get the result
output = f.get_output()
output
HeaderData Arrays
UniformGridInformation
N Cells243
N Points400
X Bounds0.000e+00, 9.000e+00
Y Bounds0.000e+00, 9.000e+00
Z Bounds0.000e+00, 3.000e+00
Dimensions10, 10, 4
Spacing1.000e+00, 1.000e+00, 1.000e+00
N Arrays3
NameFieldTypeN CompMinMax
Array 0Pointsfloat6411.013e-029.956e-01
Array 1Pointsfloat6415.150e-039.979e-01
poweredPointsfloat6412.110e-029.993e-01


arr = output["powered"]
assert np.allclose(arr, arr0**arr1)
output.plot(scalars="powered")
array math

Total running time of the script: ( 0 minutes 0.551 seconds)

Gallery generated by Sphinx-Gallery