Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more array creation, argmin and argmax, and applyOverAxis functions #149

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
950e470
feat: like methods and full function
xnought May 30, 2024
97e5ad3
feat: tests for like methods and full function
xnought May 30, 2024
1bf8856
feat: tri and eye with tests
xnought May 30, 2024
cd26537
fix: functions styling
xnought May 30, 2024
b414e84
docs: full function added to array creation
xnought May 30, 2024
23b5388
feat: random like and test
xnought May 30, 2024
42d869c
docs: *Like examples
xnought May 30, 2024
0c34c9f
docs: example of eye and tri
xnought May 30, 2024
8ef2b62
fix: colon
xnought May 30, 2024
6be8363
fix: full -> fullLike
xnought May 30, 2024
efdb317
fix: tabs to spaces
xnought May 30, 2024
8daa46b
fix: tab to space
xnought May 30, 2024
51af570
feat: argmax and argmin
xnought May 30, 2024
afc88f0
docs: argmax and argmin
xnought May 30, 2024
5c59d9a
fix: randomLike
xnought May 30, 2024
4041c51
fix: eslint mocha
xnought May 30, 2024
5dafc3e
fix: tabs to spaces
xnought May 30, 2024
ef36e6b
feat: applyOverAxis function
xnought May 31, 2024
9992c56
feat: better nestedIteration
xnought May 31, 2024
24609ee
fix: stick with var for consistency
xnought May 31, 2024
ee231e2
feat: tests for applyOverAxis
xnought May 31, 2024
c4a0ea6
feat: include print method for ease of use
xnought May 31, 2024
cc430a6
docs: applyOverAxis
xnought May 31, 2024
f4f43d8
fix: name test correctly
xnought May 31, 2024
c5bdaf3
spec
xnought May 31, 2024
0def27b
fix: tabs to spaces
xnought May 31, 2024
9fc5212
fix: tabs to spaces
xnought May 31, 2024
b8f3a22
fix: use var
xnought May 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ array([ 1, 2, 3], dtype=uint8)

__Note__: possible types are int8, uint8, int16, uint16, int32, uint32, float32, float64 and array (the default)

To create arrays with a given shape, you can use `zeros`, `ones` or `random` functions:
To create arrays with a given shape, you can use `zeros`, `ones`, `random`, and `full` functions:

```js
> nj.zeros([2,3]);
array([[ 0, 0, 0],
[ 0, 0, 0]])

> nj.ones([2,3,4], 'int32') // dtype can also be specified
array([[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
Expand All @@ -81,6 +82,38 @@ array([[ 0.9182 , 0.85176, 0.22587],
[ 0.50088, 0.74376, 0.84024],
[ 0.74045, 0.23345, 0.20289],
[ 0.00612, 0.37732, 0.06932]])

> nj.full([3,3], -3.14159)
array([[ -3.14159, -3.14159, -3.14159],
[ -3.14159, -3.14159, -3.14159],
[ -3.14159, -3.14159, -3.14159]])

> nj.full([3, 2], [1, 2])
array([[ 1, 2],
[ 1, 2],
[ 1, 2]])
```

You can also infer the shape from another array with methods like `zerosLike`, `onesLike`, `randomLike`, and `fullLike`:

Given `const arr = nj.array([[1,2], [3,4]]);` which has shape of (2,2)

```js
> nj.zerosLike(arr);
array([[ 0, 0],
[ 0, 0]])

> nj.onesLike(arr, 'int32') // dtype can also be specified
array([[ 1, 1],
[ 1, 1]], dtype=int32)

> nj.randomLike(arr)
array([[ 0.82251, 0.76331],
[ 0.22786, 0.73417]])

> nj.fullLike(arr, -3.14159)
array([[ -3.14159, -3.14159],
[ -3.14159, -3.14159]])
```

To create sequences of numbers, __NumJs__ provides a function called `arange`:
Expand All @@ -96,6 +129,25 @@ array([ 10, 15, 20, 25])
array([ 1, 2, 3, 4], dtype=uint8)
```

To generate matrices with nice properties like the identity `eye` or a triangular `tri`:

```js
> nj.eye(3, 3)
array([[ 1, 0, 0],
[ 0, 1, 0],
[ 0, 0, 1]])

> nj.tri(3, 3) // fills diagonal under k=0
array([[ 0, 0, 0],
[ 1, 0, 0],
[ 1, 1, 0]])

> nj.tri(3, 3, 1) // fills diagonal under k=1
array([[ 1, 0, 0],
[ 1, 1, 0],
[ 1, 1, 1]])
```

### More info about the array

__NumJs__’s array class is called `NdArray`. It is also known by the alias `array`. The more important properties of an `NdArray` object are:
Expand Down Expand Up @@ -369,16 +421,67 @@ array([[0.62755, 0.8278,0.21384],
> a.min()
0.2138431086204946
>
> a.argmin() // index of a.min()
[[0, 2]]
>
> a.max()
0.8278025290928781
>
> a.argmax() // index of a.max()
[[0, 1]]
>
> a.mean()
0.5187748112172509
>
> a.std()
0.22216977543691244
```

### Applying Functions over an Axis

Commonly, you might want to apply a vector function across an axis of a matrix. You can do this with the `applyOverAxis` function. For example suppose I have a matrix a

```js
> a = nj.array([[0, 1, 2],
[3, 4, 5]])
```

You can sum across the ith axis. For example the last axis is the rows, so I can specify the last axis -1

```js
> nj.applyOverAxis(a, nj.sum, { axis: -1 })
array([ 3, 12])
```

Or find the max down the columns (first axis is 0)
```js
> nj.applyOverAxis(a, nj.max, { axis: 0 })
array([ 3, 4, 5])
```

If you don't want to reduce the shape/dimensions of the output, you can put `keepdims: true`. For example when we summed across the rows I can maintain those row dimensions like

```js
> nj.applyOverAxis(a, nj.sum, { axis: -1, keepdims: true })
array([[ 3],
[ 12]])
```

This also works for tensors for example an array with (2,2,2) shape I can find the mean across the last axis:

```js
> b = array([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]]])
>
> nj.applyOverAxis(b, nj.mean, { axis: -1, keepdims: true })
array([[[ 0.5],
[ 2.5]],
[[ 4.5],
[ 6.5]]])
```

### Universal Functions
__NumJs__ provides familiar mathematical functions such as `sin`, `cos`, and `exp`. These functions operate element-wise on an array, producing an `NdArray` as output:

Expand Down
Loading