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.ImageData(dimensions=(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

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
ImageDataInformation
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 0Pointsfloat6412.346e-049.996e-01
Array 1Pointsfloat6411.743e-039.981e-01
fooPointsfloat6411.023e-011.937e+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
ImageDataInformation
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 0Pointsfloat6412.346e-049.996e-01
Array 1Pointsfloat6411.743e-039.981e-01
poweredPointsfloat6418.662e-031.000e+00


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

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

Gallery generated by Sphinx-Gallery