function arrayfun
arrayfun<
T,
A extends unknown[],
U,
>
(
x: matrix<T>,
fun: (
element: T,
...args: A,
) => U
,
...funArgs: A,
): matrix<U>

Applies a function to each element of a array or matrix.

This function applies the provided function to each element of a array or matrix. The first argument is the input array or matrix, the second is the function to apply, followed by any additional parameters required by the function.

Examples

Apply Math.log to each element of an array

import { assertEquals } from "jsr:@std/assert";

assertEquals(arrayfun([1.4, 2.3, 3], Math.log), [0.33647223662121284, 0.832909122935104, 1.0986122886681096]);

Apply the sign function to an array

import { assertEquals } from "jsr:@std/assert";
import { sign } from "../../index.ts";

assertEquals(arrayfun([1.4, 0, -10], sign), [1, 0, -1]);

Apply Math.pow to each element of a matrix

import { assertEquals } from "jsr:@std/assert";

assertEquals(arrayfun([[5, 6], [1, 3]], Math.pow, 3), [[125, 216], [1, 27]]);

Apply the sign function to each element of a matrix

import { assertEquals } from "jsr:@std/assert";
import { sign } from "../../index.ts";

assertEquals(arrayfun([[1.4, 0, -10], [2, -3, 4]], sign), [[1, 0, -1], [1, -1, 1]]);

Find substring 'cat' in each element of a nested string array

import { assertEquals } from "jsr:@std/assert";
import { strfind } from "../../index.ts";

assertEquals(
  arrayfun(
   [
     ["cat", "concat"],
     ["cattle", "catch the catfish"]
   ],
   strfind,
   "cat"
 ),
 [
   [[0], [3]],
   [[0], [0, 10]]
 ]
);

Type Parameters

A extends unknown[]

Parameters

The input array, matrix, or single value to which the function will be applied.

fun: (
element: T,
...args: A,
) => U

The function to apply to each element of x.

...funArgs: A

Return Type

The result of applying the function to each element of the input. Returns a array, matrix, or single value based on input.

Throws

If the input arguments are not valid.

arrayfun<
T,
A extends unknown[],
U,
>
(
x: array<T>,
fun: (
element: T,
...args: A,
) => U
,
...funArgs: A,
): array<U>

Applies a function to each element of a array or matrix.

This function applies the provided function to each element of a array or matrix. The first argument is the input array or matrix, the second is the function to apply, followed by any additional parameters required by the function.

Examples

Apply Math.log to each element of an array

import { assertEquals } from "jsr:@std/assert";

assertEquals(arrayfun([1.4, 2.3, 3], Math.log), [0.33647223662121284, 0.832909122935104, 1.0986122886681096]);

Apply the sign function to an array

import { assertEquals } from "jsr:@std/assert";
import { sign } from "../../index.ts";

assertEquals(arrayfun([1.4, 0, -10], sign), [1, 0, -1]);

Apply Math.pow to each element of a matrix

import { assertEquals } from "jsr:@std/assert";

assertEquals(arrayfun([[5, 6], [1, 3]], Math.pow, 3), [[125, 216], [1, 27]]);

Apply the sign function to each element of a matrix

import { assertEquals } from "jsr:@std/assert";
import { sign } from "../../index.ts";

assertEquals(arrayfun([[1.4, 0, -10], [2, -3, 4]], sign), [[1, 0, -1], [1, -1, 1]]);

Find substring 'cat' in each element of a nested string array

import { assertEquals } from "jsr:@std/assert";
import { strfind } from "../../index.ts";

assertEquals(
  arrayfun(
   [
     ["cat", "concat"],
     ["cattle", "catch the catfish"]
   ],
   strfind,
   "cat"
 ),
 [
   [[0], [3]],
   [[0], [0, 10]]
 ]
);

Type Parameters

A extends unknown[]

Parameters

The input array, matrix, or single value to which the function will be applied.

fun: (
element: T,
...args: A,
) => U

The function to apply to each element of x.

...funArgs: A

Return Type

The result of applying the function to each element of the input. Returns a array, matrix, or single value based on input.

Throws

If the input arguments are not valid.

arrayfun<
T,
A extends unknown[],
U,
>
(
x: T,
fun: (
element: T,
...args: A,
) => U
,
...funArgs: A,
): U

Applies a function to each element of a array or matrix.

This function applies the provided function to each element of a array or matrix. The first argument is the input array or matrix, the second is the function to apply, followed by any additional parameters required by the function.

Examples

Apply Math.log to each element of an array

import { assertEquals } from "jsr:@std/assert";

assertEquals(arrayfun([1.4, 2.3, 3], Math.log), [0.33647223662121284, 0.832909122935104, 1.0986122886681096]);

Apply the sign function to an array

import { assertEquals } from "jsr:@std/assert";
import { sign } from "../../index.ts";

assertEquals(arrayfun([1.4, 0, -10], sign), [1, 0, -1]);

Apply Math.pow to each element of a matrix

import { assertEquals } from "jsr:@std/assert";

assertEquals(arrayfun([[5, 6], [1, 3]], Math.pow, 3), [[125, 216], [1, 27]]);

Apply the sign function to each element of a matrix

import { assertEquals } from "jsr:@std/assert";
import { sign } from "../../index.ts";

assertEquals(arrayfun([[1.4, 0, -10], [2, -3, 4]], sign), [[1, 0, -1], [1, -1, 1]]);

Find substring 'cat' in each element of a nested string array

import { assertEquals } from "jsr:@std/assert";
import { strfind } from "../../index.ts";

assertEquals(
  arrayfun(
   [
     ["cat", "concat"],
     ["cattle", "catch the catfish"]
   ],
   strfind,
   "cat"
 ),
 [
   [[0], [3]],
   [[0], [0, 10]]
 ]
);

Type Parameters

A extends unknown[]

Parameters

x: T

The input array, matrix, or single value to which the function will be applied.

fun: (
element: T,
...args: A,
) => U

The function to apply to each element of x.

...funArgs: A

Return Type

The result of applying the function to each element of the input. Returns a array, matrix, or single value based on input.

Throws

If the input arguments are not valid.

arrayfun<
T,
A extends unknown[],
U,
>
(
x:
T
| array<T>
| matrix<T>
,
fun: (
element: T,
...args: A,
) => U
,
...funArgs: A,
):
U
| array<U>
| matrix<U>

Applies a function to each element of a array or matrix.

This function applies the provided function to each element of a array or matrix. The first argument is the input array or matrix, the second is the function to apply, followed by any additional parameters required by the function.

Examples

Apply Math.log to each element of an array

import { assertEquals } from "jsr:@std/assert";

assertEquals(arrayfun([1.4, 2.3, 3], Math.log), [0.33647223662121284, 0.832909122935104, 1.0986122886681096]);

Apply the sign function to an array

import { assertEquals } from "jsr:@std/assert";
import { sign } from "../../index.ts";

assertEquals(arrayfun([1.4, 0, -10], sign), [1, 0, -1]);

Apply Math.pow to each element of a matrix

import { assertEquals } from "jsr:@std/assert";

assertEquals(arrayfun([[5, 6], [1, 3]], Math.pow, 3), [[125, 216], [1, 27]]);

Apply the sign function to each element of a matrix

import { assertEquals } from "jsr:@std/assert";
import { sign } from "../../index.ts";

assertEquals(arrayfun([[1.4, 0, -10], [2, -3, 4]], sign), [[1, 0, -1], [1, -1, 1]]);

Find substring 'cat' in each element of a nested string array

import { assertEquals } from "jsr:@std/assert";
import { strfind } from "../../index.ts";

assertEquals(
  arrayfun(
   [
     ["cat", "concat"],
     ["cattle", "catch the catfish"]
   ],
   strfind,
   "cat"
 ),
 [
   [[0], [3]],
   [[0], [0, 10]]
 ]
);

Type Parameters

A extends unknown[]

Parameters

x:
T
| array<T>
| matrix<T>

The input array, matrix, or single value to which the function will be applied.

fun: (
element: T,
...args: A,
) => U

The function to apply to each element of x.

...funArgs: A

Return Type

U
| array<U>
| matrix<U>

The result of applying the function to each element of the input. Returns a array, matrix, or single value based on input.

Throws

If the input arguments are not valid.