Skip to content

SUBTRACT

Subtract two numeric arrays, vectors, matrices, or constants element-wise. Params: a : OrderedPair|Scalar|Vector The input a use in the subtraction of a by b. b : OrderedPair|Scalar|Vector The input b use in the subtraction of a by b. Returns: out : OrderedPair|Scalar|Vector OrderedPair if a is an OrderedPair. x: the x-axis of input a. y: the result of the subtraction of input a by input b. Scalar if a is a Scalar. c: the result of the subtraction of input a by input b. Vector if a is a Vector. v: the result of the subtraction of input a by input b.
Python Code
import numpy as np
from flojoy import flojoy, OrderedPair, Scalar, Vector
from blocks.MATH.ARITHMETIC.utils.arithmetic_utils import get_val
from functools import reduce


@flojoy
def SUBTRACT(
    a: OrderedPair | Scalar | Vector, b: list[OrderedPair | Scalar | Vector]
) -> OrderedPair | Scalar | Vector:
    """Subtract two numeric arrays, vectors, matrices, or constants element-wise.

    Parameters
    ----------
    a : OrderedPair|Scalar|Vector
        The input a use in the subtraction of a by b.
    b : OrderedPair|Scalar|Vector
        The input b use in the subtraction of a by b.

    Returns
    -------
    OrderedPair|Scalar|Vector
        OrderedPair if a is an OrderedPair.
        x: the x-axis of input a.
        y: the result of the subtraction of input a by input b.

        Scalar if a is a Scalar.
        c: the result of the subtraction of input a by input b.

        Vector if a is a Vector.
        v: the result of the subtraction of input a by input b.
    """

    initial = get_val(a)
    seq = map(lambda dc: get_val(dc), b)
    y = reduce(lambda u, v: np.subtract(u, v), seq, initial)

    match a:
        case OrderedPair():
            return OrderedPair(x=a.x, y=y)
        case Vector():
            return Vector(v=y)
        case Scalar():
            return Scalar(c=y)

Find this Flojoy Block on GitHub

Example

Having problems with this example app? Join our Discord community and we will help you out!
React Flow mini map

In this example, SUBTRACT node is used in the process of subtracting sine function.

In the result, it generated a negative sine function which is visible through SCATTER node.