numpy-ts 0.12.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -14
- package/dist/esm/common/broadcasting.js +1 -0
- package/dist/esm/common/complex.js +1 -0
- package/dist/esm/common/dtype.js +1 -0
- package/dist/esm/common/internal/compute.js +1 -0
- package/dist/esm/common/internal/indexing.js +1 -0
- package/dist/esm/common/ndarray-core.js +1 -0
- package/dist/esm/common/ops/advanced.js +1 -0
- package/dist/esm/common/ops/arithmetic.js +1 -0
- package/dist/esm/common/ops/bitwise.js +1 -0
- package/dist/esm/common/ops/comparison.js +1 -0
- package/dist/esm/common/ops/complex.js +1 -0
- package/dist/esm/common/ops/exponential.js +1 -0
- package/dist/esm/common/ops/fft.js +1 -0
- package/dist/esm/common/ops/formatting.js +4 -0
- package/dist/esm/common/ops/gradient.js +1 -0
- package/dist/esm/common/ops/hyperbolic.js +1 -0
- package/dist/esm/common/ops/linalg.js +2 -0
- package/dist/esm/common/ops/logic.js +1 -0
- package/dist/esm/common/ops/random.js +1 -0
- package/dist/esm/common/ops/reduction.js +1 -0
- package/dist/esm/common/ops/rounding.js +1 -0
- package/dist/esm/common/ops/sets.js +1 -0
- package/dist/esm/common/ops/shape.js +1 -0
- package/dist/esm/common/ops/sorting.js +1 -0
- package/dist/esm/common/ops/statistics.js +1 -0
- package/dist/esm/common/ops/trig.js +1 -0
- package/dist/esm/common/slicing.js +1 -0
- package/dist/esm/common/storage.js +1 -0
- package/dist/esm/core/advanced.js +1 -0
- package/dist/esm/core/arithmetic.js +1 -0
- package/dist/esm/core/bitwise.js +1 -0
- package/dist/esm/core/complex.js +1 -0
- package/dist/esm/core/creation.js +1 -0
- package/dist/esm/core/formatting.js +1 -0
- package/dist/esm/core/gradient.js +1 -0
- package/dist/esm/core/index.js +1 -0
- package/dist/esm/core/linalg.js +1 -0
- package/dist/esm/core/logic.js +1 -0
- package/dist/esm/core/polynomial.js +1 -0
- package/dist/esm/core/reduction.js +1 -0
- package/dist/esm/core/rounding.js +1 -0
- package/dist/esm/core/sets.js +1 -0
- package/dist/esm/core/shape-extra.js +1 -0
- package/dist/esm/core/shape.js +1 -0
- package/dist/esm/core/sorting.js +1 -0
- package/dist/esm/core/statistics.js +1 -0
- package/dist/esm/core/trig.js +1 -0
- package/dist/esm/core/typechecking.js +1 -0
- package/dist/esm/core/types.js +1 -0
- package/dist/esm/core/utility.js +1 -0
- package/dist/esm/core.js +1 -0
- package/dist/esm/full/index.js +1 -0
- package/dist/esm/full/ndarray.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/io/index.js +1 -0
- package/dist/esm/io/npy/format.js +1 -0
- package/dist/esm/io/npy/index.js +1 -0
- package/dist/esm/io/npy/parser.js +1 -0
- package/dist/esm/io/npy/serializer.js +2 -0
- package/dist/esm/io/npz/index.js +1 -0
- package/dist/esm/io/npz/parser.js +1 -0
- package/dist/esm/io/npz/serializer.js +1 -0
- package/dist/esm/io/txt/index.js +1 -0
- package/dist/esm/io/txt/parser.js +1 -0
- package/dist/esm/io/txt/serializer.js +2 -0
- package/dist/esm/io/zip/index.js +1 -0
- package/dist/esm/io/zip/reader.js +1 -0
- package/dist/esm/io/zip/types.js +1 -0
- package/dist/esm/io/zip/writer.js +1 -0
- package/dist/esm/node.js +1 -0
- package/dist/numpy-ts.browser.js +7 -2
- package/dist/numpy-ts.node-io.cjs +7 -3
- package/dist/numpy-ts.node-io.cjs.map +4 -4
- package/dist/numpy-ts.node-io.mjs +7 -3
- package/dist/numpy-ts.node-io.mjs.map +4 -4
- package/dist/numpy-ts.node.cjs +7 -2
- package/dist/numpy-ts.node.cjs.map +4 -4
- package/dist/types/common/complex.d.ts +94 -0
- package/dist/types/{internal → common/internal}/compute.d.ts +1 -1
- package/dist/types/{internal → common/internal}/indexing.d.ts +6 -0
- package/dist/types/common/ndarray-core.d.ts +126 -0
- package/dist/types/{ops → common/ops}/advanced.d.ts +2 -2
- package/dist/types/{ops → common/ops}/arithmetic.d.ts +1 -1
- package/dist/types/{ops → common/ops}/bitwise.d.ts +1 -1
- package/dist/types/{ops → common/ops}/comparison.d.ts +1 -1
- package/dist/types/{ops → common/ops}/complex.d.ts +1 -1
- package/dist/types/{ops → common/ops}/exponential.d.ts +1 -1
- package/dist/types/common/ops/fft.d.ts +166 -0
- package/dist/types/common/ops/formatting.d.ts +229 -0
- package/dist/types/{ops → common/ops}/gradient.d.ts +1 -1
- package/dist/types/{ops → common/ops}/hyperbolic.d.ts +1 -1
- package/dist/types/{ops → common/ops}/linalg.d.ts +24 -3
- package/dist/types/{ops → common/ops}/logic.d.ts +2 -2
- package/dist/types/common/ops/random.d.ts +376 -0
- package/dist/types/{ops → common/ops}/reduction.d.ts +2 -2
- package/dist/types/{ops → common/ops}/rounding.d.ts +1 -1
- package/dist/types/{ops → common/ops}/sets.d.ts +1 -1
- package/dist/types/{ops → common/ops}/shape.d.ts +1 -1
- package/dist/types/{ops → common/ops}/sorting.d.ts +1 -1
- package/dist/types/{ops → common/ops}/statistics.d.ts +1 -1
- package/dist/types/{ops → common/ops}/trig.d.ts +1 -1
- package/dist/types/{core → common}/storage.d.ts +1 -0
- package/dist/types/core/advanced.d.ts +54 -0
- package/dist/types/core/arithmetic.d.ts +110 -0
- package/dist/types/core/bitwise.d.ts +34 -0
- package/dist/types/core/complex.d.ts +15 -90
- package/dist/types/core/creation.d.ts +98 -0
- package/dist/types/core/formatting.d.ts +29 -0
- package/dist/types/core/gradient.d.ts +14 -0
- package/dist/types/core/index.d.ts +40 -0
- package/dist/types/core/linalg.d.ts +141 -0
- package/dist/types/core/logic.d.ts +72 -0
- package/dist/types/core/polynomial.d.ts +50 -0
- package/dist/types/core/reduction.d.ts +82 -0
- package/dist/types/core/rounding.d.ts +24 -0
- package/dist/types/core/sets.d.ts +48 -0
- package/dist/types/core/shape-extra.d.ts +24 -0
- package/dist/types/core/shape.d.ts +77 -0
- package/dist/types/core/sorting.d.ts +34 -0
- package/dist/types/core/statistics.d.ts +32 -0
- package/dist/types/core/trig.d.ts +58 -0
- package/dist/types/core/typechecking.d.ts +50 -0
- package/dist/types/core/types.d.ts +43 -0
- package/dist/types/core/utility.d.ts +30 -0
- package/dist/types/core.d.ts +30 -0
- package/dist/types/full/index.d.ts +603 -0
- package/dist/types/full/ndarray.d.ts +880 -0
- package/dist/types/index.d.ts +140 -26
- package/dist/types/io/npy/format.d.ts +1 -1
- package/dist/types/io/npy/parser.d.ts +3 -3
- package/dist/types/io/npy/serializer.d.ts +2 -2
- package/dist/types/io/npz/parser.d.ts +4 -4
- package/dist/types/io/npz/serializer.d.ts +6 -6
- package/dist/types/io/txt/parser.d.ts +5 -5
- package/dist/types/io/txt/serializer.d.ts +2 -2
- package/dist/types/node.d.ts +12 -8
- package/package.json +30 -29
- package/dist/numpy-ts.esm.js +0 -2
- package/dist/types/core/ndarray.d.ts +0 -3923
- package/dist/types/ops/random.d.ts +0 -136
- /package/dist/types/{core → common}/broadcasting.d.ts +0 -0
- /package/dist/types/{core → common}/dtype.d.ts +0 -0
- /package/dist/types/{core → common}/slicing.d.ts +0 -0
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|

|
|
4
4
|
[](https://www.npmjs.com/package/numpy-ts)
|
|
5
5
|

|
|
6
|
-

|
|
7
7
|
|
|
8
8
|
```
|
|
9
9
|
███╗ ██╗██╗ ██╗███╗ ███╗██████╗ ██╗ ██╗ ████████╗███████╗
|
|
@@ -24,11 +24,14 @@ npm install numpy-ts
|
|
|
24
24
|
|
|
25
25
|
## Why numpy-ts?
|
|
26
26
|
|
|
27
|
-
- **📊 Extensive API** — **
|
|
28
|
-
- **✅ NumPy-validated** —
|
|
27
|
+
- **📊 Extensive API** — **476 of 507 NumPy functions (93.9% coverage)**
|
|
28
|
+
- **✅ NumPy-validated** — 6,000+ test cases cross-validated against Python NumPy
|
|
29
29
|
- **🔒 Type-safe** — Full TypeScript support with shape and dtype inference
|
|
30
30
|
- **🌐 Universal** — Works in Node.js and browsers with .npy/.npz file support
|
|
31
31
|
- **🎯 Zero dependencies** — Pure TypeScript, no heavy external libraries
|
|
32
|
+
- **🌳 Tree-shakeable** — Import only what you need (~11KB for `zeros`, not 180KB)
|
|
33
|
+
|
|
34
|
+
> **💡 Bundle size tip:** Standalone functions like `sin(arr)` tree-shake well. Method chaining like `arr.sin()` is convenient but pulls in the full library. See [modular imports guide](docs/modular-imports.mdx).
|
|
32
35
|
|
|
33
36
|
## Quick Start
|
|
34
37
|
|
|
@@ -69,15 +72,19 @@ Progress toward complete NumPy API compatibility:
|
|
|
69
72
|
| **Broadcasting** | 3/3 | 100% | ✅ |
|
|
70
73
|
| **Comparison** | 10/10 | 100% | ✅ |
|
|
71
74
|
| **Exponential** | 9/9 | 100% | ✅ |
|
|
75
|
+
| **FFT** | 18/18 | 100% | ✅ |
|
|
72
76
|
| **Gradient** | 4/4 | 100% | ✅ |
|
|
73
77
|
| **Hyperbolic** | 9/9 | 100% | ✅ |
|
|
74
78
|
| **I/O** | 8/8 | 100% | ✅ |
|
|
75
79
|
| **Indexing** | 21/21 | 100% | ✅ |
|
|
76
|
-
| **Linear Algebra** |
|
|
80
|
+
| **Linear Algebra** | 16/16 | 100% | ✅ |
|
|
77
81
|
| **Linear Algebra (linalg)** | 31/31 | 100% | ✅ |
|
|
78
82
|
| **Logic** | 24/24 | 100% | ✅ |
|
|
83
|
+
| **NDArray Methods** | 47/47 | 100% | ✅ |
|
|
79
84
|
| **Other Math** | 15/15 | 100% | ✅ |
|
|
80
85
|
| **Polynomials** | 10/10 | 100% | ✅ |
|
|
86
|
+
| **Printing/Formatting** | 10/10 | 100% | ✅ |
|
|
87
|
+
| **Random** | 53/53 | 100% | ✅ |
|
|
81
88
|
| **Reductions** | 36/36 | 100% | ✅ |
|
|
82
89
|
| **Rounding** | 7/7 | 100% | ✅ |
|
|
83
90
|
| **Searching** | 7/7 | 100% | ✅ |
|
|
@@ -86,14 +93,10 @@ Progress toward complete NumPy API compatibility:
|
|
|
86
93
|
| **Statistics** | 11/11 | 100% | ✅ |
|
|
87
94
|
| **Trigonometric** | 16/16 | 100% | ✅ |
|
|
88
95
|
| **Type Checking** | 7/7 | 100% | ✅ |
|
|
89
|
-
| **
|
|
90
|
-
| **
|
|
91
|
-
| **Random** | 17/53 | 32% | 🔴 |
|
|
92
|
-
| **FFT** | 0/18 | 0% | 🔴 |
|
|
93
|
-
| **String/Formatting** | 0/10 | 0% | 🔴 |
|
|
94
|
-
| **Unplanned** | 0/26 | 0% | 🔴 |
|
|
96
|
+
| **Utilities** | 10/10 | 100% | ✅ |
|
|
97
|
+
| **Unplanned** | 0/31 | 0% | 🔴 |
|
|
95
98
|
|
|
96
|
-
**Overall:
|
|
99
|
+
**Overall: 476/507 functions (93.9% coverage)**
|
|
97
100
|
|
|
98
101
|
See the complete [API Reference](docs/API-REFERENCE.md) for detailed function list.
|
|
99
102
|
|
|
@@ -140,6 +143,7 @@ numpy-ts focuses on numeric array computing. The following NumPy features are **
|
|
|
140
143
|
| **F-order memory layout** | Exists in NumPy for Fortran/BLAS interop, which doesn't exist in JS |
|
|
141
144
|
| **Object dtype** (`object_`) | Defeats the purpose of typed arrays; use regular JS arrays instead |
|
|
142
145
|
| **String/Bytes dtypes** (`str_`, `bytes_`, `U`, `S`) | JS strings are first-class; no need for fixed-width string arrays |
|
|
146
|
+
| **Exact random sequence reproduction** | numpy-ts random functions produce statistically correct distributions but use different transformation algorithms (Box-Muller vs Ziggurat), so seeded sequences won't match NumPy exactly |
|
|
143
147
|
|
|
144
148
|
These omissions keep the library focused and the bundle small. For string manipulation, datetime math, or heterogeneous data, use native JS/TS constructs alongside numpy-ts.
|
|
145
149
|
|
|
@@ -256,12 +260,12 @@ arr.sum(); // Type: number
|
|
|
256
260
|
|
|
257
261
|
| Feature | numpy-ts | numjs | ndarray | TensorFlow.js |
|
|
258
262
|
|---------|----------|-------|---------|---------------|
|
|
259
|
-
| NumPy API Coverage |
|
|
263
|
+
| NumPy API Coverage | 476/507 (94%) | ~20% | Different | ML-focused |
|
|
260
264
|
| TypeScript Native | ✅ Full | Partial | ❌ No | ✅ Yes |
|
|
261
|
-
| NumPy Validated | ✅
|
|
265
|
+
| NumPy Validated | ✅ 6,000+ tests | Mostly | ❌ No | ❌ No |
|
|
262
266
|
| .npy/.npz Files | ✅ v1/v2/v3 | ❌ No | ❌ No | ❌ No |
|
|
263
267
|
| Broadcasting | ✅ Full | Limited | Limited | ✅ Full |
|
|
264
|
-
| Bundle Size | <
|
|
268
|
+
| Bundle Size | <100kb | ~60kb | ~5kb | >100kb |
|
|
265
269
|
|
|
266
270
|
## Contributing
|
|
267
271
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as d}from"./storage";function c(r){if(r.length===0)return[];if(r.length===1)return Array.from(r[0]);const n=Math.max(...r.map(t=>t.length)),e=new Array(n);for(let t=0;t<n;t++){let o=1;for(const i of r){const a=i.length-n+t,s=a<0?1:i[a];if(s!==1){if(o===1)o=s;else if(o!==s)return null}}e[t]=o}return e}function h(r,n){return c([Array.from(r),Array.from(n)])!==null}function m(r,n,e){const t=r.length,o=e.length,i=new Array(o).fill(0);for(let a=0;a<t;a++){const s=o-t+a,l=r[a],u=e[s];if(l===u)i[s]=n[a];else if(l===1)i[s]=0;else throw new Error("Invalid broadcast")}return i}function f(r,n){const e=m(r.shape,r.strides,n);return d.fromData(r.data,Array.from(n),r.dtype,e,r.offset)}function p(r){if(r.length===0)return[];if(r.length===1)return r;const n=r.map(t=>Array.from(t.shape)),e=c(n);if(e===null)throw new Error(`operands could not be broadcast together with shapes ${n.map(t=>JSON.stringify(t)).join(" ")}`);return r.map(t=>f(t,e))}function y(...r){const n=c(r);if(n===null){const e=r.map(t=>`(${t.join(",")})`).join(" ");throw new Error(`shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between ${e}`)}return n}function g(r,n){const e=n?` for ${n}`:"",t=r.map(o=>`(${o.join(",")})`).join(" ");return`operands could not be broadcast together${e} with shapes ${t}`}export{h as areBroadcastable,p as broadcastArrays,g as broadcastErrorMessage,y as broadcastShapes,f as broadcastTo,c as computeBroadcastShape};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class r{constructor(i,e=0){this.re=i,this.im=e}abs(){return Math.sqrt(this.re*this.re+this.im*this.im)}angle(){return Math.atan2(this.im,this.re)}conj(){return new r(this.re,-this.im)}add(i){return typeof i=="number"?new r(this.re+i,this.im):new r(this.re+i.re,this.im+i.im)}sub(i){return typeof i=="number"?new r(this.re-i,this.im):new r(this.re-i.re,this.im-i.im)}mul(i){return typeof i=="number"?new r(this.re*i,this.im*i):new r(this.re*i.re-this.im*i.im,this.re*i.im+this.im*i.re)}div(i){if(typeof i=="number")return new r(this.re/i,this.im/i);const e=i.re*i.re+i.im*i.im;return new r((this.re*i.re+this.im*i.im)/e,(this.im*i.re-this.re*i.im)/e)}neg(){return new r(-this.re,-this.im)}equals(i){return this.re===i.re&&this.im===i.im}toString(){return this.im===0?`(${this.re}+0j)`:this.im<0?`(${this.re}${this.im}j)`:`(${this.re}+${this.im}j)`}static from(i){if(i instanceof r)return i;if(typeof i=="number")return new r(i,0);if(Array.isArray(i))return new r(i[0]??0,i[1]??0);if(typeof i=="object"&&i!==null&&"re"in i)return new r(i.re??0,i.im??0);throw new Error(`Cannot convert ${i} to Complex`)}static isComplex(i){return i instanceof r||typeof i=="object"&&i!==null&&"re"in i&&"im"in i}}function t(n){return n instanceof r||typeof n=="object"&&n!==null&&"re"in n}export{r as Complex,t as isComplexLike};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const x="float64";function A(r){switch(r){case"float64":return Float64Array;case"float32":return Float32Array;case"complex128":return Float64Array;case"complex64":return Float32Array;case"int64":return BigInt64Array;case"int32":return Int32Array;case"int16":return Int16Array;case"int8":return Int8Array;case"uint64":return BigUint64Array;case"uint32":return Uint32Array;case"uint16":return Uint16Array;case"uint8":return Uint8Array;case"bool":return Uint8Array;default:throw new Error(`Unknown dtype: ${r}`)}}function D(r){switch(r){case"complex128":return 16;case"float64":case"int64":case"uint64":case"complex64":return 8;case"float32":case"int32":case"uint32":return 4;case"int16":case"uint16":return 2;case"int8":case"uint8":case"bool":return 1;default:throw new Error(`Unknown dtype: ${r}`)}}function y(r){return r==="int64"||r==="int32"||r==="int16"||r==="int8"||r==="uint64"||r==="uint32"||r==="uint16"||r==="uint8"}function p(r){return r==="float64"||r==="float32"}function m(r){return r==="int64"||r==="uint64"}function e(r){return r==="complex64"||r==="complex128"}function b(r,t,u){if(e(r)){const f=u?` ${u}`:"";throw new TypeError(`ufunc '${t}' not supported for complex dtype '${r}'.${f}`)}}function g(r,t){if(e(r))throw new Error(`'${t}' does not yet support complex dtype '${r}'. Complex support is planned but not yet implemented.`)}function C(r){if(r==="complex128")return"float64";if(r==="complex64")return"float32";throw new Error(`${r} is not a complex dtype`)}function w(r){if(r==="float64")return"complex128";if(r==="float32")return"complex64";throw new Error(`${r} is not a valid complex component dtype`)}function T(r){return typeof r=="object"&&r!==null&&"re"in r&&"im"in r}function I(r){return typeof r=="bigint"?"int64":typeof r=="number"?Number.isInteger(r)?r>=0&&r<=255?"uint8":r>=-128&&r<=127?"int8":r>=0&&r<=65535?"uint16":r>=-32768&&r<=32767?"int16":r>=0&&r<=4294967295?"uint32":r>=-2147483648&&r<=2147483647?"int32":"float64":"float64":typeof r=="boolean"?"bool":T(r)?"complex128":x}function U(r,t){if(r===t)return r;if(r==="bool")return t;if(t==="bool")return r;if(e(r)||e(t)){if(e(r)&&e(t))return r==="complex128"||t==="complex128"?"complex128":"complex64";const n=e(r)?r:t,a=e(r)?t:r;return n==="complex128"||a==="float64"||a==="int64"||a==="uint64"||a==="int32"||a==="uint32"?"complex128":"complex64"}if(p(r)||p(t)){if(r==="float64"||t==="float64")return"float64";if(r==="float32"){const n=t;return n==="int32"||n==="int64"||n==="uint32"||n==="uint64"?"float64":"float32"}if(t==="float32"){const n=r;return n==="int32"||n==="int64"||n==="uint32"||n==="uint64"?"float64":"float32"}return"float32"}const u=r.startsWith("int"),f=t.startsWith("int"),c=r.startsWith("uint"),s=t.startsWith("uint"),l=n=>n.includes("64")?64:n.includes("32")?32:n.includes("16")?16:n.includes("8")?8:0,i=l(r),o=l(t);if(r==="int64"&&t==="uint64"||r==="uint64"&&t==="int64")return"float64";if(u&&s&&i===o){if(i===8)return"int16";if(i===16)return"int32";if(i===32)return"int64"}if(c&&f&&i===o){if(o===8)return"int16";if(o===16)return"int32";if(o===32)return"int64"}if(u&&f||c&&s){const n=Math.max(i,o);return u?n===64?"int64":n===32?"int32":n===16?"int16":"int8":n===64?"uint64":n===32?"uint32":n===16?"uint16":"uint8"}return u&&s?i>o?r:o===8?"int16":o===16?"int32":o===32?"int64":"float64":c&&f?o>i?t:i===8?"int16":i===16?"int32":i===32?"int64":"float64":"float64"}function h(r){return["float64","float32","complex128","complex64","int64","int32","int16","int8","uint64","uint32","uint16","uint8","bool"].includes(r)}function F(r,t){return m(t)?BigInt(r):t==="bool"?r?1:0:Number(r)}function $(r){return r==="int64"||r==="uint64"||e(r)?"generic":r}export{x as DEFAULT_DTYPE,F as castValue,C as getComplexComponentDType,w as getComplexDType,D as getDTypeSize,A as getTypedArrayConstructor,I as inferDType,m as isBigIntDType,e as isComplexDType,T as isComplexLike,p as isFloatDType,y as isIntegerDType,h as isValidDType,U as promoteDTypes,b as throwIfComplex,g as throwIfComplexNotImplemented,$ as toStdlibDType};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as B}from"../storage";import{promoteDTypes as C,isBigIntDType as h}from"../dtype";import{Complex as I}from"../complex";function z(t,r){const l=t.length,o=r.length,c=Math.max(l,o),s=new Array(c);for(let a=0;a<c;a++){const d=a<c-l?1:t[a-(c-l)],m=a<c-o?1:r[a-(c-o)];if(d===m)s[a]=d;else if(d===1)s[a]=m;else if(m===1)s[a]=d;else throw new Error(`operands could not be broadcast together with shapes ${JSON.stringify(Array.from(t))} ${JSON.stringify(Array.from(r))}`)}return s}function x(t,r,l){const o=t.length,c=l.length,s=new Array(c).fill(0);for(let a=0;a<o;a++){const d=c-o+a,m=t[a],u=l[d];if(m===u)s[d]=r[a];else if(m===1)s[d]=0;else throw new Error("Invalid broadcast")}return s}function D(t,r){const l=x(t.shape,t.strides,r);return B.fromData(t.data,Array.from(r),t.dtype,l,t.offset)}function M(t,r,l,o){const c=C(t.dtype,r.dtype),s=t.shape,a=r.shape;if(s.length===a.length&&s.every((n,i)=>n===a[i])&&t.isCContiguous&&r.isCContiguous&&!h(t.dtype)&&!h(r.dtype)&&!h(c)){const n=t.size,i=B.zeros(Array.from(s),c),g=i.data,p=t.data,S=r.data,N=t.offset,w=r.offset;if(N===0&&w===0)for(let f=0;f<n;f++)g[f]=l(p[f],S[f]);else for(let f=0;f<n;f++)g[f]=l(p[N+f],S[w+f]);return i}const m=z(t.shape,r.shape),u=D(t,m),y=D(r,m),b=B.zeros(m,c),A=b.data,e=b.size;if(h(c)){const n=A;for(let i=0;i<e;i++){const g=u.iget(i),p=y.iget(i),S=g instanceof I?g.re:g,N=p instanceof I?p.re:p,w=typeof S=="bigint"?S:BigInt(Math.round(S)),f=typeof N=="bigint"?N:BigInt(Math.round(N));o==="add"?n[i]=w+f:o==="subtract"?n[i]=w-f:o==="multiply"?n[i]=w*f:o==="divide"?n[i]=w/f:n[i]=BigInt(Math.round(l(Number(w),Number(f))))}}else{const n=h(t.dtype)||h(r.dtype);for(let i=0;i<e;i++){const g=u.iget(i),p=y.iget(i),S=Number(g),N=Number(p);A[i]=l(S,N)}}return b}function R(t,r,l){const o=z(t.shape,r.shape),c=D(t,o),s=D(r,o),a=o.reduce((u,y)=>u*y,1),d=new Uint8Array(a),m=h(t.dtype)||h(r.dtype);for(let u=0;u<a;u++){const y=c.iget(u),b=s.iget(u),A=Number(y),e=Number(b);d[u]=l(A,e)?1:0}return B.fromData(d,o,"bool")}function V(t,r,l=!0){const o=t.dtype,c=Array.from(t.shape),s=t.size,d=l?o:o!=="float32"&&o!=="float64"?"float64":o,m=B.zeros(c,d),u=m.data,y=t.data,b=t.offset,A=t.isCContiguous;if(h(o))if(h(d)){const e=u;if(A)for(let n=0;n<s;n++){const i=Number(y[b+n]);e[n]=BigInt(Math.round(r(i)))}else for(let n=0;n<s;n++)e[n]=BigInt(Math.round(r(Number(t.iget(n)))))}else if(A)for(let e=0;e<s;e++)u[e]=r(Number(y[b+e]));else for(let e=0;e<s;e++)u[e]=r(Number(t.iget(e)));else if(A)if(b===0)for(let e=0;e<s;e++)u[e]=r(Number(y[e]));else for(let e=0;e<s;e++)u[e]=r(Number(y[b+e]));else for(let e=0;e<s;e++)u[e]=r(Number(t.iget(e)));return m}export{z as broadcastShapes,M as elementwiseBinaryOp,R as elementwiseComparisonOp,V as elementwiseUnaryOp};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function d(r){const t=new Array(r.length);let u=1;for(let e=r.length-1;e>=0;e--)t[e]=u,u*=r[e];return t}function f(r,t){let u=0,e=1;for(let n=r.length-1;n>=0;n--)u+=r[n]*e,e*=t[n];return u}function c(r,t,u){let e=u;for(let n=0;n<r.length;n++)e+=r[n]*t[n];return e}function x(r,t,u,e){const n=e.length,l=new Array(n),m=Array.from(e).filter((o,b)=>b!==t);let i=r;for(let o=m.length-1;o>=0;o--)l[o>=t?o+1:o]=i%m[o],i=Math.floor(i/m[o]);return l[t]=u,l}export{d as computeStrides,c as multiIndexToBuffer,f as multiIndexToLinear,x as outerIndexToMultiIndex};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{parseSlice as c,normalizeSlice as d}from"./slicing";import{getTypedArrayConstructor as m,getDTypeSize as b,isBigIntDType as y,isComplexDType as A}from"./dtype";import{Complex as f}from"./complex";import{ArrayStorage as p}from"./storage";import{array_str as _}from"./ops/formatting";class l{constructor(t,r){this._storage=t,this._base=r}get storage(){return this._storage}static fromStorage(t,r){return new l(t,r)}get shape(){return this._storage.shape}get ndim(){return this._storage.ndim}get size(){return this._storage.size}get dtype(){return this._storage.dtype}get data(){return this._storage.data}get strides(){return this._storage.strides}get flags(){return{C_CONTIGUOUS:this._storage.isCContiguous,F_CONTIGUOUS:this._storage.isFContiguous,OWNDATA:this._base===void 0}}get base(){return this._base??null}get itemsize(){return b(this._storage.dtype)}get nbytes(){return this.size*this.itemsize}fill(t){const r=this._storage.dtype,i=this.size;if(y(r)){const n=typeof t=="bigint"?t:BigInt(Math.round(Number(t)));for(let e=0;e<i;e++)this._storage.iset(e,n)}else if(r==="bool"){const n=t?1:0;for(let e=0;e<i;e++)this._storage.iset(e,n)}else{const n=Number(t);for(let e=0;e<i;e++)this._storage.iset(e,n)}}*[Symbol.iterator](){if(this.ndim===0)yield this._storage.iget(0);else if(this.ndim===1)for(let t=0;t<this.shape[0];t++)yield this._storage.iget(t);else for(let t=0;t<this.shape[0];t++)yield this.slice(String(t))}get(t){if(t.length!==this.ndim)throw new Error(`Index has ${t.length} dimensions, but array has ${this.ndim} dimensions`);const r=t.map((i,n)=>{let e=i;if(e<0&&(e=this.shape[n]+e),e<0||e>=this.shape[n])throw new Error(`Index ${i} is out of bounds for axis ${n} with size ${this.shape[n]}`);return e});return this._storage.get(...r)}set(t,r){if(t.length!==this.ndim)throw new Error(`Index has ${t.length} dimensions, but array has ${this.ndim} dimensions`);const i=t.map((e,a)=>{let o=e;if(o<0&&(o=this.shape[a]+o),o<0||o>=this.shape[a])throw new Error(`Index ${e} is out of bounds for axis ${a} with size ${this.shape[a]}`);return o}),n=this.dtype;if(A(n))this._storage.set(i,r);else if(y(n)){const e=r instanceof f?r.re:Number(r),a=typeof r=="bigint"?r:BigInt(Math.round(e));this._storage.set(i,a)}else if(n==="bool"){const a=(r instanceof f?r.re:Number(r))?1:0;this._storage.set(i,a)}else{const e=r instanceof f?r.re:Number(r);this._storage.set(i,e)}}iget(t){return this._storage.iget(t)}iset(t,r){this._storage.iset(t,r)}copy(){return new l(this._storage.copy())}astype(t,r=!0){const i=this.dtype;if(i===t&&!r)return this;if(i===t&&r)return this.copy();const n=Array.from(this.shape),e=this.size,a=m(t);if(!a)throw new Error(`Cannot convert to dtype ${t}`);const o=new a(e),g=this.data;if(y(i)&&!y(t)){const h=g;if(t==="bool")for(let s=0;s<e;s++)o[s]=h[s]!==BigInt(0)?1:0;else for(let s=0;s<e;s++)o[s]=Number(h[s])}else if(!y(i)&&y(t)){const h=g;for(let s=0;s<e;s++)o[s]=BigInt(Math.round(Number(h[s])))}else if(t==="bool"){const h=g;for(let s=0;s<e;s++)o[s]=h[s]!==0?1:0}else if(i==="bool"&&!y(t)){const h=g;for(let s=0;s<e;s++)o[s]=h[s]}else if(!y(i)&&!y(t)){const h=g;for(let s=0;s<e;s++)o[s]=h[s]}else{const h=g;for(let s=0;s<e;s++)o[s]=h[s]}const u=p.fromData(o,n,t);return new l(u)}slice(...t){if(t.length===0)return this;if(t.length>this.ndim)throw new Error(`Too many indices for array: array is ${this.ndim}-dimensional, but ${t.length} were indexed`);const r=t.map((g,u)=>{const h=c(g);return d(h,this.shape[u])});for(;r.length<this.ndim;)r.push({start:0,stop:this.shape[r.length],step:1,isIndex:!1});const i=[],n=[];let e=this._storage.offset;for(let g=0;g<r.length;g++){const u=r[g];if(e+=u.start*this._storage.strides[g],u.step===0)continue;const h=Math.max(0,Math.ceil((u.stop-u.start)/u.step));i.push(h),n.push(this._storage.strides[g]*u.step)}const a=p.fromData(this._storage.data,i,this._storage.dtype,n,e),o=this._base??this;return new l(a,o)}toString(){return _(this._storage)}toArray(){if(this.ndim===0)return this._storage.iget(0);const t=this.shape,r=t.length,i=(n,e)=>{if(e===r)return this._storage.get(...n);const a=[];for(let o=0;o<t[e];o++)n[e]=o,a.push(i(n,e+1));return a};return i(new Array(r),0)}tolist(){return this.toArray()}tobytes(){if(this._storage.isCContiguous){const i=this._storage.data,n=i.BYTES_PER_ELEMENT,e=this._storage.offset*n,a=this.size*n;return i.buffer.slice(e,e+a)}const r=this.copy()._storage.data;return r.buffer.slice(0,this.size*r.BYTES_PER_ELEMENT)}item(...t){if(t.length===0){if(this.size!==1)throw new Error("can only convert an array of size 1 to a Python scalar");return this._storage.iget(0)}if(t.length===1){const r=t[0];if(r<0||r>=this.size)throw new Error(`index ${r} is out of bounds for size ${this.size}`);return this._storage.iget(r)}return this.get(t)}}export{l as NDArrayCore};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as w,computeStrides as N}from"../storage";import{getTypedArrayConstructor as U,isBigIntDType as $}from"../dtype";import{computeBroadcastShape as j,broadcastTo as T,broadcastShapes as q}from"../broadcasting";import{Complex as O}from"../complex";function Y(r,t){const a=r.shape,s=a.length,i=t.length;if(i<s)throw new Error("input operand has more dimensions than allowed by the axis remapping");const o=j([Array.from(a),t]);if(o===null)throw new Error(`operands could not be broadcast together with shape (${a.join(",")}) (${t.join(",")})`);for(let e=0;e<i;e++)if(o[e]!==t[e])throw new Error(`operands could not be broadcast together with shape (${a.join(",")}) (${t.join(",")})`);return T(r,t)}function Z(r){if(r.length===0)return[];if(r.length===1)return[r[0]];const t=r.map(s=>Array.from(s.shape)),a=j(t);if(a===null)throw new Error(`operands could not be broadcast together with shapes ${t.map(s=>`(${s.join(",")})`).join(" ")}`);return r.map(s=>T(s,a))}function rr(r,t,a){const s=r.shape,i=s.length,o=r.dtype;if(a===void 0){const p=r.size;for(const B of t){const x=B<0?p+B:B;if(x<0||x>=p)throw new Error(`index ${B} is out of bounds for axis 0 with size ${p}`)}const g=t.length,A=U(o);if(!A)throw new Error(`Cannot take from array with dtype ${o}`);const I=new A(g);for(let B=0;B<g;B++){let x=t[B];x<0&&(x=p+x);const m=r.iget(x);$(o),I[B]=m}return w.fromData(I,[g],o)}const e=a<0?i+a:a;if(e<0||e>=i)throw new Error(`axis ${a} is out of bounds for array of dimension ${i}`);const n=s[e];for(const p of t){const g=p<0?n+p:p;if(g<0||g>=n)throw new Error(`index ${p} is out of bounds for axis ${e} with size ${n}`)}const u=Array.from(s);u[e]=t.length;const d=u.reduce((p,g)=>p*g,1),l=U(o);if(!l)throw new Error(`Cannot take from array with dtype ${o}`);const f=new l(d),c=N(u),y=new Array(i).fill(0);for(let p=0;p<d;p++){const g=[...y];let A=y[e],I=t[A];I<0&&(I=n+I),g[e]=I;const B=r.get(...g);let x=0;for(let m=0;m<i;m++)x+=y[m]*c[m];$(o),f[x]=B;for(let m=i-1;m>=0&&(y[m]++,!(y[m]<u[m]));m--)y[m]=0}return w.fromData(f,u,o)}function tr(r,t,a){const s=r.size,i=r.dtype;let o;if(typeof a=="number"||typeof a=="bigint")o=new Array(t.length).fill(a);else{o=[];for(let e=0;e<a.size;e++){const n=a.iget(e);o.push(n instanceof O?n.re:n)}if(o.length===1)o=new Array(t.length).fill(o[0]);else if(o.length!==t.length){const e=[...o];o=[];for(let n=0;n<t.length;n++)o.push(e[n%e.length])}}for(let e=0;e<t.length;e++){let n=t[e];if(n<0&&(n=s+n),n<0||n>=s)throw new Error(`index ${t[e]} is out of bounds for axis 0 with size ${s}`);let u=o[e];$(i)?typeof u!="bigint"&&(u=BigInt(Math.round(Number(u)))):typeof u=="bigint"&&(u=Number(u)),r.iset(n,u)}}function er(r,t){if(t.length===0)throw new Error("choices cannot be empty");const a=r.shape,s=t.length,i=t[0].dtype,o=t.map(c=>Array.from(c.shape));o.unshift(Array.from(a));const e=j(o);if(e===null)throw new Error("operands could not be broadcast together");const n=T(r,e),u=t.map(c=>T(c,e)),d=e.reduce((c,y)=>c*y,1),l=U(i);if(!l)throw new Error(`Cannot choose with dtype ${i}`);const f=new l(d);for(let c=0;c<d;c++){const y=Number(n.iget(c));if(y<0||y>=s)throw new Error(`index ${y} is out of bounds for axis 0 with size ${s}`);const p=u[y].iget(c);$(i),f[c]=p}return w.fromData(f,e,i)}function or(r,t,a=!1){if(r.ndim!==t.ndim)return!1;for(let i=0;i<r.ndim;i++)if(r.shape[i]!==t.shape[i])return!1;const s=r.size;for(let i=0;i<s;i++){const o=r.iget(i),e=t.iget(i);if(a){const n=typeof o=="number"&&Number.isNaN(o),u=typeof e=="number"&&Number.isNaN(e);if(n&&u)continue}if(o!==e)return!1}return!0}function nr(r,t,a){const s=r.shape,i=s.length,o=r.dtype,e=a<0?i+a:a;if(e<0||e>=i)throw new Error(`axis ${a} is out of bounds for array of dimension ${i}`);const n=t.shape;if(n.length!==i)throw new Error(`indices and arr must have the same number of dimensions, got ${n.length} vs ${i}`);for(let g=0;g<i;g++)if(g!==e&&n[g]!==s[g]&&n[g]!==1&&s[g]!==1)throw new Error(`index ${n[g]} is out of bounds for size ${s[g]} in dimension ${g}`);const u=Array.from(n),d=u.reduce((g,A)=>g*A,1),l=U(o);if(!l)throw new Error(`Cannot take_along_axis with dtype ${o}`);const f=new l(d),c=N(s),y=N(n),p=s[e];for(let g=0;g<d;g++){const A=new Array(i);let I=g;for(let b=i-1;b>=0;b--)A[b]=I%u[b],I=Math.floor(I/u[b]);let B=0;for(let b=0;b<i;b++){const h=n[b]===1?0:A[b];B+=h*y[b]}let x=Number(t.iget(B));if(x<0&&(x=p+x),x<0||x>=p)throw new Error(`index ${x} is out of bounds for axis ${e} with size ${p}`);const m=[...A];m[e]=x;let S=0;for(let b=0;b<i;b++){const h=s[b]===1?0:m[b];S+=h*c[b]}const z=r.iget(S);$(o),f[g]=z}return w.fromData(f,u,o)}function ir(r,t,a,s){const i=r.shape,o=i.length,e=r.dtype,n=s<0?o+s:s;if(n<0||n>=o)throw new Error(`axis ${s} is out of bounds for array of dimension ${o}`);const u=t.shape,d=a.shape;if(u.length!==o||d.length!==o)throw new Error("indices, arr, and values must have same ndim");const l=i[n],f=N(i),c=N(u),y=N(d),p=u.reduce((g,A)=>g*A,1);for(let g=0;g<p;g++){const A=new Array(o);let I=g;for(let h=o-1;h>=0;h--)A[h]=I%u[h],I=Math.floor(I/u[h]);let B=0;for(let h=0;h<o;h++)B+=A[h]*c[h];let x=Number(t.iget(B));if(x<0&&(x=l+x),x<0||x>=l)throw new Error(`index ${x} is out of bounds for axis ${n} with size ${l}`);let m=0;for(let h=0;h<o;h++){const C=d[h]===1?0:A[h];m+=C*y[h]}let S=a.iget(m);const z=[...A];z[n]=x;let b=0;for(let h=0;h<o;h++)b+=z[h]*f[h];$(e)?typeof S!="bigint"&&(S=BigInt(Math.round(Number(S)))):typeof S=="bigint"&&(S=Number(S)),r.iset(b,S)}}function ar(r,t,a){const s=r.size,i=r.dtype;let o;if(typeof a=="number"||typeof a=="bigint")o=[a];else{o=[];for(let n=0;n<a.size;n++){const u=a.iget(n);o.push(u instanceof O?u.re:u)}}let e=0;for(let n=0;n<s;n++)if(t.iget(n)){let d=o[e%o.length];$(i)?typeof d!="bigint"&&(d=BigInt(Math.round(Number(d)))):typeof d=="bigint"&&(d=Number(d)),r.iset(n,d),e++}}function sr(r,t,a){const s=t.shape,i=s.length,o=t.dtype,e=$(o);if(a===void 0){const m=Math.min(r.size,t.size),S=r.isCContiguous,z=r.data,b=r.offset;let h=0;if(S)for(let E=0;E<m;E++)z[b+E]&&h++;else for(let E=0;E<m;E++)r.iget(E)&&h++;const C=U(o);if(!C)throw new Error(`Cannot compress with dtype ${o}`);const _=new C(h);let D=0;const k=t.isCContiguous;if(S&&k){const E=t.data,V=t.offset;if(e){const L=_,F=E;for(let v=0;v<m;v++)z[b+v]&&(L[D++]=F[V+v])}else{const L=_,F=E;for(let v=0;v<m;v++)z[b+v]&&(L[D++]=F[V+v])}}else for(let E=0;E<m;E++)(S?z[b+E]:r.iget(E))&&(_[D]=t.iget(E),D++);return w.fromData(_,[h],o)}const n=a<0?i+a:a;if(n<0||n>=i)throw new Error(`axis ${a} is out of bounds for array of dimension ${i}`);const u=s[n],d=Math.min(r.size,u),l=[];for(let m=0;m<d;m++)r.iget(m)&&l.push(m);const f=l.length,c=[...s];c[n]=f;const y=c.reduce((m,S)=>m*S,1),p=U(o);if(!p)throw new Error(`Cannot compress with dtype ${o}`);const g=new p(y),A=s.slice(n+1).reduce((m,S)=>m*S,1),I=s.slice(0,n).reduce((m,S)=>m*S,1);let B=0;if(t.isCContiguous){const m=t.data,S=t.offset;if(e){const z=g,b=m;for(let h=0;h<I;h++)for(let C=0;C<f;C++){const _=l[C];for(let D=0;D<A;D++){const k=h*u*A+_*A+D;z[B++]=b[S+k]}}}else{const z=g,b=m;for(let h=0;h<I;h++)for(let C=0;C<f;C++){const _=l[C];for(let D=0;D<A;D++){const k=h*u*A+_*A+D;z[B++]=b[S+k]}}}}else for(let m=0;m<I;m++)for(let S=0;S<f;S++){const z=l[S];for(let b=0;b<A;b++){const h=m*u*A+z*A+b;g[B++]=t.iget(h)}}return w.fromData(g,c,o)}function ur(r,t,a=0){if(r.length!==t.length)throw new Error("condlist and choicelist must have same length");if(r.length===0)throw new Error("condlist and choicelist cannot be empty");const s=[...r.map(c=>Array.from(c.shape)),...t.map(c=>Array.from(c.shape))],i=j(s);if(i===null)throw new Error("condlist and choicelist arrays could not be broadcast together");const o=t[0].dtype,e=i.reduce((c,y)=>c*y,1),n=U(o);if(!n)throw new Error(`Cannot select with dtype ${o}`);let u=a;$(o)?u=typeof a=="bigint"?a:BigInt(a):u=typeof a=="bigint"?Number(a):a;const d=new n(e);for(let c=0;c<e;c++)$(o),d[c]=u;const l=r.map(c=>T(c,i)),f=t.map(c=>T(c,i));for(let c=0;c<e;c++)for(let y=0;y<r.length;y++)if(l[y].iget(c)){const p=f[y].iget(c);$(o),d[c]=p;break}return w.fromData(d,i,o)}function fr(r,t,a){const s=r.size,i=r.dtype,o=[];for(let n=0;n<a.size;n++){const u=a.iget(n);o.push(u instanceof O?u.re:u)}if(o.length===0)return;let e=0;for(let n=0;n<s;n++)if(t.iget(n)){let d=o[e%o.length];$(i)?typeof d!="bigint"&&(d=BigInt(Math.round(Number(d)))):typeof d=="bigint"&&(d=Number(d)),r.iset(n,d),e++}}function R(r,t=2){if(t<1)throw new Error("ndim must be at least 1");const a=new Int32Array(r);for(let i=0;i<r;i++)a[i]=i;const s=[];for(let i=0;i<t;i++)s.push(w.fromData(new Int32Array(a),[r],"int32"));return s}function lr(r){const t=r.shape,a=t.length;if(a<2)throw new Error("array must be at least 2-D");const s=t[0];for(let i=1;i<a;i++)if(t[i]!==s)throw new Error("All dimensions of input must be equal");return R(s,a)}function G(r,t=0,a){const s=a??r,i=[],o=[];for(let e=0;e<r;e++)for(let n=0;n<=Math.min(e+t,s-1);n++)n>=0&&(i.push(e),o.push(n));return[w.fromData(new Int32Array(i),[i.length],"int32"),w.fromData(new Int32Array(o),[o.length],"int32")]}function cr(r,t=0){const a=r.shape;if(a.length!==2)throw new Error("array must be 2-D");return G(a[0],t,a[1])}function H(r,t=0,a){const s=a??r,i=[],o=[];for(let e=0;e<r;e++)for(let n=Math.max(e+t,0);n<s;n++)i.push(e),o.push(n);return[w.fromData(new Int32Array(i),[i.length],"int32"),w.fromData(new Int32Array(o),[o.length],"int32")]}function dr(r,t=0){const a=r.shape;if(a.length!==2)throw new Error("array must be 2-D");return H(a[0],t,a[1])}function yr(r,t,a=0){const s=t(r,a),i=s.shape;if(i.length!==2||i[0]!==r||i[1]!==r)throw new Error("mask_func must return n x n array");const o=[],e=[];for(let n=0;n<r;n++)for(let u=0;u<r;u++)s.get(n,u)&&(o.push(n),e.push(u));return[w.fromData(new Int32Array(o),[o.length],"int32"),w.fromData(new Int32Array(e),[e.length],"int32")]}function pr(r,t="int32"){const a=r.length,s=[a,...r],i=s.reduce((u,d)=>u*d,1),o=U(t);if(!o)throw new Error(`Cannot create indices with dtype ${t}`);const e=new o(i),n=r.reduce((u,d)=>u*d,1);for(let u=0;u<a;u++){const d=u*n;for(let l=0;l<n;l++){const f=new Array(a);let c=l;for(let p=a-1;p>=0;p--)f[p]=c%r[p],c=Math.floor(c/r[p]);const y=f[u];t==="int64"?e[d+l]=BigInt(y):e[d+l]=y}}return w.fromData(e,s,t)}function gr(...r){const t=r.length,a=[];for(let s=0;s<t;s++){const i=r[s],o=i.size,e=i.dtype,n=new Array(t).fill(1);n[s]=o;const u=U(e);if(!u)throw new Error(`Cannot create ix_ with dtype ${e}`);const d=new u(o);for(let l=0;l<o;l++){const f=i.iget(l);$(e),d[l]=f}a.push(w.fromData(d,n,e))}return a}function mr(r,t,a="raise"){if(r.length!==t.length)throw new Error("multi_index length must equal dims length");if(r.length===0)throw new Error("multi_index cannot be empty");const s=r[0].size,i=t.length,o=new Int32Array(s),e=new Array(i);let n=1;for(let u=i-1;u>=0;u--)e[u]=n,n*=t[u];for(let u=0;u<s;u++){let d=0;for(let l=0;l<i;l++){let f=Number(r[l].iget(u));const c=t[l];if(a==="wrap")f=(f%c+c)%c;else if(a==="clip")f=Math.max(0,Math.min(f,c-1));else if(f<0||f>=c)throw new Error(`index ${f} is out of bounds for axis ${l} with size ${c}`);d+=f*e[l]}o[u]=d}return w.fromData(o,[s],"int32")}function hr(r,t,a="C"){const s=t.length;let i,o;if(typeof r=="number")i=[r],o=[];else{i=[];for(let l=0;l<r.size;l++)i.push(Number(r.iget(l)));o=Array.from(r.shape)}const e=i.length,n=t.reduce((l,f)=>l*f,1),u=new Array(s);if(a==="C"){let l=1;for(let f=s-1;f>=0;f--)u[f]=l,l*=t[f]}else{let l=1;for(let f=0;f<s;f++)u[f]=l,l*=t[f]}const d=[];for(let l=0;l<s;l++){const f=new Int32Array(e);d.push(w.fromData(f,o.length?o:[1],"int32"))}for(let l=0;l<e;l++){let f=i[l];if(f<0||f>=n)throw new Error(`index ${f} is out of bounds for array with size ${n}`);if(a==="C")for(let c=0;c<s;c++){const y=Math.floor(f/u[c]);f=f%u[c],d[c].data[l]=y%t[c]}else for(let c=s-1;c>=0;c--){const y=Math.floor(f/u[c]);f=f%u[c],d[c].data[l]=y%t[c]}}return typeof r=="number"?d.map(l=>{const f=l.iget(0);return w.fromData(new Int32Array([Number(f)]),[],"int32")}):d}function Ar(r,t,a=!1){const s=r.shape,i=s.length;if(i<2)throw new Error("array must be at least 2-d");let o;if(i===2)o=s[1]+1;else{o=1;for(let d=1;d<i;d++){let l=1;for(let f=d;f<i;f++)l*=s[f];o+=l}}const e=r.size;let n=Math.min(...s);a&&i===2&&(n=Math.max(s[0],s[1]));const u=r.isCContiguous;if(typeof t=="number")if(u){const d=r.data,l=r.offset;if($(r.dtype)){const f=d,c=BigInt(Math.round(t));for(let y=0;y<n;y++){const p=y*o;if(p>=e)break;f[l+p]=c}}else for(let f=0;f<n;f++){const c=f*o;if(c>=e)break;d[l+c]=t}}else for(let d=0;d<n&&d*o<e;d++)r.iset(d*o,t);else{const d=t.size;if(u&&t.isCContiguous){const l=r.data,f=r.offset,c=t.data,y=t.offset;if($(r.dtype)){const p=l,g=c;for(let A=0;A<n;A++){const I=A*o;if(I>=e)break;p[f+I]=g[y+A%d]}}else{const p=l,g=c;for(let A=0;A<n;A++){const I=A*o;if(I>=e)break;p[f+I]=g[y+A%d]}}}else for(let l=0;l<n&&l*o<e;l++)r.iset(l*o,t.iget(l%d))}}function br(r,t,a){const s=Array.from(r.shape),i=s.length;if(t<0&&(t+=i),t<0||t>=i)throw new Error(`axis ${t} is out of bounds for array of dimension ${i}`);const o=[];for(let e=0;e<i;e++)e!==t&&o.push(s[e]);if(o.length===0){const e=a(r);if(typeof e=="number"){const n=w.zeros([1],r.dtype);return n.data[0]=e,n}return e}if(i===2){const[e,n]=s;if(t===0){const u=[];for(let l=0;l<n;l++){const f=new Float64Array(e);for(let y=0;y<e;y++)f[y]=Number(r.get(y,l));const c=w.fromData(f,[e],"float64");u.push(a(c))}const d=u[0];if(d===void 0)return w.zeros([0],"float64");if(typeof d=="number"){const l=w.zeros([n],"float64");for(let f=0;f<n;f++)l.data[f]=u[f];return l}else{const l=[d.size,n],f=w.zeros(l,"float64");for(let c=0;c<n;c++){const y=u[c];for(let p=0;p<y.size;p++)f.data[p*n+c]=Number(y.iget(p))}return f}}else{const u=[];for(let l=0;l<e;l++){const f=new Float64Array(n);for(let y=0;y<n;y++)f[y]=Number(r.get(l,y));const c=w.fromData(f,[n],"float64");u.push(a(c))}const d=u[0];if(d===void 0)return w.zeros([0],"float64");if(typeof d=="number"){const l=w.zeros([e],"float64");for(let f=0;f<e;f++)l.data[f]=u[f];return l}else{const l=[e,d.size],f=w.zeros(l,"float64");for(let c=0;c<e;c++){const y=u[c];for(let p=0;p<y.size;p++)f.data[c*y.size+p]=Number(y.iget(p))}return f}}}if(i===1){const e=a(r);if(typeof e=="number"){const n=w.zeros([1],"float64");return n.data[0]=e,n}return e}throw new Error(`apply_along_axis not fully implemented for ${i}D arrays. Only 1D and 2D arrays are supported.`)}function wr(r,t,a){let s=r;const i=r.shape.length;for(const o of a){let e=o<0?o+i:o;if(e<0||e>=i)throw new Error(`axis ${o} is out of bounds for array of dimension ${i}`);if(s=t(s,e),s.shape.length<i){const n=Array.from(s.shape);n.splice(e,0,1);const u=N(n);s=new w(s.data,n,u,0,s.dtype)}}return s}function J(r,t){return r.data.buffer===t.data.buffer}function xr(r,t){return J(r,t)}let M={divide:"warn",over:"warn",under:"ignore",invalid:"warn"};function K(){return{...M}}function Sr(r,t,a,s,i){const o=K();return r!==void 0&&(M.divide=r,M.over=r,M.under=r,M.invalid=r),t!==void 0&&(M.divide=t),a!==void 0&&(M.over=a),s!==void 0&&(M.under=s),i!==void 0&&(M.invalid=i),o}export{br as apply_along_axis,wr as apply_over_axes,or as array_equal,Z as broadcast_arrays,q as broadcast_shapes,Y as broadcast_to,er as choose,sr as compress,R as diag_indices,lr as diag_indices_from,Ar as fill_diagonal,K as geterr,pr as indices,gr as ix_,yr as mask_indices,J as may_share_memory,fr as place,tr as put,ir as put_along_axis,ar as putmask,mr as ravel_multi_index,ur as select,Sr as seterr,xr as shares_memory,rr as take,nr as take_along_axis,G as tril_indices,cr as tril_indices_from,H as triu_indices,dr as triu_indices_from,hr as unravel_index};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as b}from"../storage";import{isBigIntDType as N,isComplexDType as M,getComplexComponentDType as j,promoteDTypes as x,throwIfComplex as S}from"../dtype";import{elementwiseBinaryOp as D}from"../internal/compute";function P(t,r){return t.isCContiguous&&r.isCContiguous&&t.shape.length===r.shape.length&&t.shape.every((f,i)=>f===r.shape[i])}function T(t,r){return[t[r*2],t[r*2+1]]}function F(t,r,f,i){t[r*2]=f,t[r*2+1]=i}function at(t,r){if(typeof r=="number")return G(t,r);if(r.size===1&&!M(r.dtype)&&t.dtype===r.dtype){const f=Number(r.iget(0));return G(t,f)}return P(t,r)?k(t,r):D(t,r,(f,i)=>f+i,"add")}function k(t,r){const f=x(t.dtype,r.dtype),i=b.zeros(Array.from(t.shape),f),m=t.size,u=t.data,c=r.data,l=i.data,a=t.offset,e=r.offset;if(M(f)){const s=l,o=M(t.dtype),n=M(r.dtype);for(let y=0;y<m;y++){const[p,d]=o?T(u,a+y):[Number(u[a+y]),0],[A,g]=n?T(c,e+y):[Number(c[e+y]),0];F(s,y,p+A,d+g)}return i}if(N(f)){const s=l;if(!N(t.dtype)||!N(r.dtype))for(let n=0;n<m;n++){const y=typeof u[a+n]=="bigint"?u[a+n]:BigInt(Math.round(Number(u[a+n]))),p=typeof c[e+n]=="bigint"?c[e+n]:BigInt(Math.round(Number(c[e+n])));s[n]=y+p}else{const n=u,y=c;for(let p=0;p<m;p++)s[p]=n[a+p]+y[e+p]}}else if(N(t.dtype)||N(r.dtype))for(let o=0;o<m;o++){const n=typeof u[a+o]=="bigint"?Number(u[a+o]):u[a+o],y=typeof c[e+o]=="bigint"?Number(c[e+o]):c[e+o];l[o]=n+y}else if(a===0&&e===0)for(let o=0;o<m;o++)l[o]=u[o]+c[o];else for(let o=0;o<m;o++)l[o]=u[a+o]+c[e+o];return i}function nt(t,r){if(typeof r=="number")return H(t,r);if(r.size===1&&!M(r.dtype)&&t.dtype===r.dtype){const f=Number(r.iget(0));return H(t,f)}return P(t,r)?J(t,r):D(t,r,(f,i)=>f-i,"subtract")}function J(t,r){const f=x(t.dtype,r.dtype),i=b.zeros(Array.from(t.shape),f),m=t.size,u=t.data,c=r.data,l=i.data,a=t.offset,e=r.offset;if(M(f)){const s=l,o=M(t.dtype),n=M(r.dtype);for(let y=0;y<m;y++){const[p,d]=o?T(u,a+y):[Number(u[a+y]),0],[A,g]=n?T(c,e+y):[Number(c[e+y]),0];F(s,y,p-A,d-g)}return i}if(N(f)){const s=l;if(!N(t.dtype)||!N(r.dtype))for(let n=0;n<m;n++){const y=typeof u[a+n]=="bigint"?u[a+n]:BigInt(Math.round(Number(u[a+n]))),p=typeof c[e+n]=="bigint"?c[e+n]:BigInt(Math.round(Number(c[e+n])));s[n]=y-p}else{const n=u,y=c;for(let p=0;p<m;p++)s[p]=n[a+p]-y[e+p]}}else if(N(t.dtype)||N(r.dtype))for(let o=0;o<m;o++){const n=typeof u[a+o]=="bigint"?Number(u[a+o]):u[a+o],y=typeof c[e+o]=="bigint"?Number(c[e+o]):c[e+o];l[o]=n-y}else if(a===0&&e===0)for(let o=0;o<m;o++)l[o]=u[o]-c[o];else for(let o=0;o<m;o++)l[o]=u[a+o]-c[e+o];return i}function it(t,r){if(typeof r=="number")return X(t,r);if(r.size===1&&!M(r.dtype)&&t.dtype===r.dtype){const f=Number(r.iget(0));return X(t,f)}return P(t,r)?K(t,r):D(t,r,(f,i)=>f*i,"multiply")}function K(t,r){const f=x(t.dtype,r.dtype),i=b.zeros(Array.from(t.shape),f),m=t.size,u=t.data,c=r.data,l=i.data,a=t.offset,e=r.offset;if(M(f)){const s=l,o=M(t.dtype),n=M(r.dtype);for(let y=0;y<m;y++){const[p,d]=o?T(u,a+y):[Number(u[a+y]),0],[A,g]=n?T(c,e+y):[Number(c[e+y]),0],h=p*A-d*g,C=p*g+d*A;F(s,y,h,C)}return i}if(N(f)){const s=l;if(!N(t.dtype)||!N(r.dtype))for(let n=0;n<m;n++){const y=typeof u[a+n]=="bigint"?u[a+n]:BigInt(Math.round(Number(u[a+n]))),p=typeof c[e+n]=="bigint"?c[e+n]:BigInt(Math.round(Number(c[e+n])));s[n]=y*p}else{const n=u,y=c;for(let p=0;p<m;p++)s[p]=n[a+p]*y[e+p]}}else if(N(t.dtype)||N(r.dtype))for(let o=0;o<m;o++){const n=typeof u[a+o]=="bigint"?Number(u[a+o]):u[a+o],y=typeof c[e+o]=="bigint"?Number(c[e+o]):c[e+o];l[o]=n*y}else if(a===0&&e===0)for(let o=0;o<m;o++)l[o]=u[o]*c[o];else for(let o=0;o<m;o++)l[o]=u[a+o]*c[e+o];return i}function ft(t,r){if(typeof r=="number")return Q(t,r);const f=M(t.dtype),i=M(r.dtype);if(f||i){const s=x(t.dtype,r.dtype),o=b.zeros(Array.from(t.shape),s),n=o.data,y=t.size,p=t.data,d=r.data,A=t.offset,g=r.offset;for(let h=0;h<y;h++){const[C,w]=f?T(p,A+h):[Number(p[A+h]),0],[v,U]=i?T(d,g+h):[Number(d[g+h]),0],I=v*v+U*U,z=(C*v+w*U)/I,B=(w*v-C*U)/I;F(n,h,z,B)}return o}const m=t.dtype==="float64",u=r.dtype==="float64",c=t.dtype==="float32",l=r.dtype==="float32";if(m||u){const s=m?t:q(t,"float64"),o=u?r:q(r,"float64");return D(s,o,(n,y)=>n/y,"divide")}if(c||l){const s=c?t:q(t,"float32"),o=l?r:q(r,"float32");return D(s,o,(n,y)=>n/y,"divide")}const a=q(t,"float64"),e=q(r,"float64");return D(a,e,(s,o)=>s/o,"divide")}function q(t,r){const f=b.zeros(Array.from(t.shape),r),i=t.size,m=f.data;if(t.isCContiguous){const u=t.data,c=t.offset;for(let l=0;l<i;l++)m[l]=Number(u[c+l])}else for(let u=0;u<i;u++)m[u]=Number(t.iget(u));return f}function G(t,r){const f=t.dtype,i=Array.from(t.shape),m=t.data,u=t.size,c=t.offset,l=t.isCContiguous,a=b.zeros(i,f),e=a.data;if(M(f)){const s=m,o=e;if(l)for(let n=0;n<u;n++){const[y,p]=T(s,c+n);F(o,n,y+r,p)}else for(let n=0;n<u;n++){const y=t.iget(n),p=y.re??Number(y),d=y.im??0;F(o,n,p+r,d)}}else if(N(f)){const s=e,o=BigInt(Math.round(r));if(l){const n=m;for(let y=0;y<u;y++)s[y]=n[c+y]+o}else for(let n=0;n<u;n++)s[n]=t.iget(n)+o}else if(l)if(c===0)for(let s=0;s<u;s++)e[s]=Number(m[s])+r;else for(let s=0;s<u;s++)e[s]=Number(m[c+s])+r;else for(let s=0;s<u;s++)e[s]=Number(t.iget(s))+r;return a}function H(t,r){const f=t.dtype,i=Array.from(t.shape),m=t.data,u=t.size,c=t.offset,l=t.isCContiguous,a=b.zeros(i,f),e=a.data;if(M(f)){const s=m,o=e;if(l)for(let n=0;n<u;n++){const[y,p]=T(s,c+n);F(o,n,y-r,p)}else for(let n=0;n<u;n++){const y=t.iget(n),p=y.re??Number(y),d=y.im??0;F(o,n,p-r,d)}}else if(N(f)){const s=e,o=BigInt(Math.round(r));if(l){const n=m;for(let y=0;y<u;y++)s[y]=n[c+y]-o}else for(let n=0;n<u;n++)s[n]=t.iget(n)-o}else if(l)for(let s=0;s<u;s++)e[s]=Number(m[c+s])-r;else for(let s=0;s<u;s++)e[s]=Number(t.iget(s))-r;return a}function X(t,r){const f=t.dtype,i=Array.from(t.shape),m=t.data,u=t.size,c=t.offset,l=t.isCContiguous,a=b.zeros(i,f),e=a.data;if(M(f)){const s=m,o=e;if(l)for(let n=0;n<u;n++){const[y,p]=T(s,c+n);F(o,n,y*r,p*r)}else for(let n=0;n<u;n++){const y=t.iget(n),p=y.re??Number(y),d=y.im??0;F(o,n,p*r,d*r)}}else if(N(f)){const s=e,o=BigInt(Math.round(r));if(l){const n=m;for(let y=0;y<u;y++)s[y]=n[c+y]*o}else for(let n=0;n<u;n++)s[n]=t.iget(n)*o}else if(l)if(c===0)for(let s=0;s<u;s++)e[s]=Number(m[s])*r;else for(let s=0;s<u;s++)e[s]=Number(m[c+s])*r;else for(let s=0;s<u;s++)e[s]=Number(t.iget(s))*r;return a}function Q(t,r){const f=t.dtype,i=Array.from(t.shape),m=t.data,u=t.size,c=t.offset,l=t.isCContiguous;if(M(f)){const n=b.zeros(i,f),y=n.data;if(l){const p=m;for(let d=0;d<u;d++){const[A,g]=T(p,c+d);F(y,d,A/r,g/r)}}else for(let p=0;p<u;p++){const d=t.iget(p),A=d.re??Number(d),g=d.im??0;F(y,p,A/r,g/r)}return n}const e=f!=="float32"&&f!=="float64"?"float64":f,s=b.zeros(i,e),o=s.data;if(l)for(let n=0;n<u;n++)o[n]=Number(m[c+n])/r;else for(let n=0;n<u;n++)o[n]=Number(t.iget(n))/r;return s}function ut(t){const r=t.dtype,f=Array.from(t.shape),i=t.data,m=t.size,u=t.offset,c=t.isCContiguous;if(M(r)){const e=j(r),s=b.zeros(f,e),o=s.data;if(c){const n=i;for(let y=0;y<m;y++){const p=n[(u+y)*2],d=n[(u+y)*2+1];o[y]=Math.sqrt(p*p+d*d)}}else for(let n=0;n<m;n++){const y=t.iget(n),p=y.re??Number(y),d=y.im??0;o[n]=Math.sqrt(p*p+d*d)}return s}const l=b.zeros(f,r),a=l.data;if(N(r)){const e=a;if(c){const s=i;for(let o=0;o<m;o++){const n=s[u+o];e[o]=n<0n?-n:n}}else for(let s=0;s<m;s++){const o=t.iget(s);e[s]=o<0n?-o:o}}else if(c)if(u===0)for(let e=0;e<m;e++)a[e]=Math.abs(Number(i[e]));else for(let e=0;e<m;e++)a[e]=Math.abs(Number(i[u+e]));else for(let e=0;e<m;e++)a[e]=Math.abs(Number(t.iget(e)));return l}function ct(t){const r=t.dtype,f=Array.from(t.shape),i=t.data,m=t.size,u=t.offset,c=t.isCContiguous,l=b.zeros(f,r),a=l.data;if(M(r)){const e=a;if(c){const s=i;for(let o=0;o<m;o++){const[n,y]=T(s,u+o);F(e,o,-n,-y)}}else for(let s=0;s<m;s++){const o=t.iget(s),n=o.re??Number(o),y=o.im??0;F(e,s,-n,-y)}}else if(N(r)){const e=a;if(c){const s=i;for(let o=0;o<m;o++)e[o]=-s[u+o]}else for(let s=0;s<m;s++)e[s]=-t.iget(s)}else if(c)if(u===0)for(let e=0;e<m;e++)a[e]=-Number(i[e]);else for(let e=0;e<m;e++)a[e]=-Number(i[u+e]);else for(let e=0;e<m;e++)a[e]=-Number(t.iget(e));return l}function lt(t){S(t.dtype,"sign","Sign is not defined for complex numbers.");const r=t.dtype,f=Array.from(t.shape),i=t.data,m=t.size,u=t.offset,c=t.isCContiguous,l=b.zeros(f,r),a=l.data;if(N(r)){const e=a;if(c){const s=i;for(let o=0;o<m;o++){const n=s[u+o];e[o]=n>0n?1n:n<0n?-1n:0n}}else for(let s=0;s<m;s++){const o=t.iget(s);e[s]=o>0n?1n:o<0n?-1n:0n}}else if(c)for(let e=0;e<m;e++){const s=Number(i[u+e]);a[e]=s>0?1:s<0?-1:0}else for(let e=0;e<m;e++){const s=Number(t.iget(e));a[e]=s>0?1:s<0?-1:0}return l}function $(t,r){return S(t.dtype,"mod","Modulo is not defined for complex numbers."),typeof r!="number"&&S(r.dtype,"mod","Modulo is not defined for complex numbers."),typeof r=="number"?W(t,r):D(t,r,(f,i)=>(f%i+i)%i,"mod")}function W(t,r){const f=t.dtype,i=Array.from(t.shape),m=t.data,u=t.size,c=t.offset,l=t.isCContiguous,a=b.zeros(i,f),e=a.data;if(N(f)){const s=e,o=BigInt(Math.round(r));if(l){const n=m;for(let y=0;y<u;y++){const p=n[c+y];s[y]=(p%o+o)%o}}else for(let n=0;n<u;n++){const y=t.iget(n);s[n]=(y%o+o)%o}}else if(l)for(let s=0;s<u;s++){const o=Number(m[c+s]);e[s]=(o%r+r)%r}else for(let s=0;s<u;s++){const o=Number(t.iget(s));e[s]=(o%r+r)%r}return a}function Y(t,r){return S(t.dtype,"floor_divide","Floor division is not defined for complex numbers."),typeof r!="number"&&S(r.dtype,"floor_divide","Floor division is not defined for complex numbers."),typeof r=="number"?Z(t,r):D(t,r,(f,i)=>Math.floor(f/i),"floor_divide")}function Z(t,r){const f=t.dtype,i=Array.from(t.shape),m=t.data,u=t.size,c=t.offset,l=t.isCContiguous,a=b.zeros(i,f),e=a.data;if(N(f)){const s=e,o=BigInt(Math.round(r));if(l){const n=m;for(let y=0;y<u;y++)s[y]=n[c+y]/o}else for(let n=0;n<u;n++)s[n]=t.iget(n)/o}else if(l)for(let s=0;s<u;s++)e[s]=Math.floor(Number(m[c+s])/r);else for(let s=0;s<u;s++)e[s]=Math.floor(Number(t.iget(s))/r);return a}function yt(t){const r=t.dtype,f=Array.from(t.shape),i=t.data,m=t.size,u=t.offset,c=t.isCContiguous,l=b.zeros(f,r),a=l.data;if(M(r)){const e=a;if(c){const s=i;for(let o=0;o<m;o++)e[o*2]=s[(u+o)*2],e[o*2+1]=s[(u+o)*2+1]}else for(let s=0;s<m;s++){const o=t.iget(s);e[s*2]=o.re??Number(o),e[s*2+1]=o.im??0}}else if(c)for(let e=0;e<m;e++)a[e]=i[u+e];else for(let e=0;e<m;e++)a[e]=t.iget(e);return l}function mt(t){const r=t.dtype,f=Array.from(t.shape),i=t.data,m=t.size,u=t.offset,c=t.isCContiguous;if(M(r)){const o=b.zeros(f,r),n=o.data;if(c){const y=i;for(let p=0;p<m;p++){const d=y[(u+p)*2],A=y[(u+p)*2+1],g=d*d+A*A;n[p*2]=d/g,n[p*2+1]=-A/g}}else for(let y=0;y<m;y++){const p=t.iget(y),d=p.re??Number(p),A=p.im??0,g=d*d+A*A;n[y*2]=d/g,n[y*2+1]=-A/g}return o}const a=r!=="float32"&&r!=="float64"?"float64":r,e=b.zeros(f,a),s=e.data;if(c)if(u===0)for(let o=0;o<m;o++)s[o]=1/Number(i[o]);else for(let o=0;o<m;o++)s[o]=1/Number(i[u+o]);else for(let o=0;o<m;o++)s[o]=1/Number(t.iget(o));return e}function pt(t){const r=t.dtype;S(r,"cbrt","cbrt is not supported for complex numbers.");const f=Array.from(t.shape),i=t.data,m=t.size,u=t.offset,l=r!=="float32"&&r!=="float64"?"float64":r,a=b.zeros(f,l),e=a.data;if(t.isCContiguous)for(let s=0;s<m;s++)e[s]=Math.cbrt(Number(i[u+s]));else for(let s=0;s<m;s++)e[s]=Math.cbrt(Number(t.iget(s)));return a}function dt(t){const r=t.dtype;S(r,"fabs","fabs is only for real numbers. Use absolute() for complex.");const f=Array.from(t.shape),i=t.data,m=t.size,u=t.offset,c=r==="float32"?"float32":"float64",l=b.zeros(f,c),a=l.data;if(t.isCContiguous)if(u===0)for(let e=0;e<m;e++)a[e]=Math.abs(Number(i[e]));else for(let e=0;e<m;e++)a[e]=Math.abs(Number(i[u+e]));else for(let e=0;e<m;e++)a[e]=Math.abs(Number(t.iget(e)));return l}function gt(t,r){const f=Y(t,r),i=$(t,r);return[f,i]}function At(t){const r=t.dtype,f=Array.from(t.shape),i=t.data,m=t.size,u=b.zeros(f,r),c=u.data;if(M(r)){const l=i,a=c;for(let e=0;e<m;e++){const s=l[e*2],o=l[e*2+1];a[e*2]=s*s-o*o,a[e*2+1]=2*s*o}}else if(N(r)){const l=i,a=c;for(let e=0;e<m;e++)a[e]=l[e]*l[e]}else for(let l=0;l<m;l++){const a=Number(i[l]);c[l]=a*a}return u}function bt(t,r){return $(t,r)}function ht(t,r){S(t.dtype,"heaviside","Heaviside step function is not defined for complex numbers."),typeof r!="number"&&S(r.dtype,"heaviside","Heaviside step function is not defined for complex numbers.");const f=t.dtype,i=Array.from(t.shape),m=t.size,u=f==="float32"?"float32":"float64",c=b.zeros(i,u),l=c.data;if(typeof r=="number")for(let a=0;a<m;a++){const e=Number(t.iget(a));e<0?l[a]=0:e===0?l[a]=r:l[a]=1}else{const a=r.shape;if(i.every((e,s)=>e===a[s]))for(let e=0;e<m;e++){const s=Number(t.iget(e));s<0?l[e]=0:s===0?l[e]=Number(r.iget(e)):l[e]=1}else for(let e=0;e<m;e++){const s=Number(t.iget(e)),o=e%r.size;s<0?l[e]=0:s===0?l[e]=Number(r.iget(o)):l[e]=1}}return c}function St(t,r){const f=t.dtype;if(M(f)){const i=t.size,m=b.zeros(Array.from(t.shape),f),u=m.data;if(typeof r=="number")if(t.isCContiguous){const c=t.data,l=t.offset;for(let a=0;a<i;a++){const e=c[(l+a)*2],s=c[(l+a)*2+1],o=Math.hypot(e,s),n=Math.atan2(s,e),y=Math.pow(o,r),p=n*r;u[a*2]=y*Math.cos(p),u[a*2+1]=y*Math.sin(p)}}else for(let c=0;c<i;c++){const l=t.iget(c),a=Math.hypot(l.re,l.im),e=Math.atan2(l.im,l.re),s=Math.pow(a,r),o=e*r;u[c*2]=s*Math.cos(o),u[c*2+1]=s*Math.sin(o)}else{const c=M(r.dtype);if(t.isCContiguous&&r.isCContiguous){const l=t.data,a=t.offset,e=r.data,s=r.offset;for(let o=0;o<i;o++){const n=l[(a+o)*2],y=l[(a+o)*2+1];let p,d;if(c){const I=e;p=I[(s+o)*2],d=I[(s+o)*2+1]}else p=Number(e[s+o]),d=0;const A=Math.hypot(n,y),g=Math.atan2(y,n),h=Math.log(A),C=g,w=p*h-d*C,v=p*C+d*h,U=Math.exp(w);u[o*2]=U*Math.cos(v),u[o*2+1]=U*Math.sin(v)}}else for(let l=0;l<i;l++){const a=t.iget(l);let e,s;if(c){const h=r.iget(l);e=h.re,s=h.im}else e=Number(r.iget(l)),s=0;const o=Math.hypot(a.re,a.im),n=Math.atan2(a.im,a.re),y=Math.log(o),p=n,d=e*y-s*p,A=e*p+s*y,g=Math.exp(d);u[l*2]=g*Math.cos(A),u[l*2+1]=g*Math.sin(A)}}return m}if(typeof r=="number"){const i=b.zeros(Array.from(t.shape),"float64"),m=i.data,u=t.size;if(t.isCContiguous){const c=t.data,l=t.offset;for(let a=0;a<u;a++)m[a]=Math.pow(Number(c[l+a]),r)}else for(let c=0;c<u;c++)m[c]=Math.pow(Number(t.iget(c)),r);return i}return D(t,r,(i,m)=>Math.pow(i,m),"float_power")}function Nt(t,r){if(S(t.dtype,"fmod","fmod is not defined for complex numbers."),typeof r!="number"&&S(r.dtype,"fmod","fmod is not defined for complex numbers."),typeof r=="number"){const f=t.copy(),i=f.data,m=t.size;for(let u=0;u<m;u++){const c=Number(i[u]);i[u]=c-Math.trunc(c/r)*r}return f}return D(t,r,(f,i)=>f-Math.trunc(f/i)*i,"fmod")}function zt(t){S(t.dtype,"frexp","frexp is not defined for complex numbers.");const r=b.zeros(Array.from(t.shape),"float64"),f=b.zeros(Array.from(t.shape),"int32"),i=r.data,m=f.data,u=t.size;for(let c=0;c<u;c++){const l=Number(t.iget(c));if(l===0||!isFinite(l))i[c]=l,m[c]=0;else{const a=Math.floor(Math.log2(Math.abs(l)))+1,e=l/Math.pow(2,a);i[c]=e,m[c]=a}}return[r,f]}function Mt(t,r){S(t.dtype,"gcd","GCD is only defined for integers."),typeof r!="number"&&S(r.dtype,"gcd","GCD is only defined for integers.");const f=(l,a)=>{for(l=Math.abs(Math.trunc(l)),a=Math.abs(Math.trunc(a));a!==0;){const e=a;a=l%a,l=e}return l};if(typeof r=="number"){const l=b.zeros(Array.from(t.shape),"int32"),a=l.data,e=t.size,s=Math.abs(Math.trunc(r));if(t.isCContiguous){const o=t.data,n=t.offset;for(let y=0;y<e;y++)a[y]=f(Number(o[n+y]),s)}else for(let o=0;o<e;o++)a[o]=f(Number(t.iget(o)),s);return l}const i=D(t,r,f,"gcd"),m=b.zeros(Array.from(i.shape),"int32"),u=m.data,c=i.size;if(i.isCContiguous){const l=i.data,a=i.offset;for(let e=0;e<c;e++)u[e]=Math.round(Number(l[a+e]))}else for(let l=0;l<c;l++)u[l]=Math.round(Number(i.iget(l)));return m}function It(t,r){S(t.dtype,"lcm","LCM is only defined for integers."),typeof r!="number"&&S(r.dtype,"lcm","LCM is only defined for integers.");const f=(a,e)=>{for(a=Math.abs(Math.trunc(a)),e=Math.abs(Math.trunc(e));e!==0;){const s=e;e=a%e,a=s}return a},i=(a,e)=>(a=Math.abs(Math.trunc(a)),e=Math.abs(Math.trunc(e)),a===0||e===0?0:a*e/f(a,e));if(typeof r=="number"){const a=b.zeros(Array.from(t.shape),"int32"),e=a.data,s=t.size,o=Math.abs(Math.trunc(r));if(t.isCContiguous){const n=t.data,y=t.offset;for(let p=0;p<s;p++)e[p]=i(Number(n[y+p]),o)}else for(let n=0;n<s;n++)e[n]=i(Number(t.iget(n)),o);return a}const m=D(t,r,i,"lcm"),u=b.zeros(Array.from(m.shape),"int32"),c=u.data,l=m.size;if(m.isCContiguous){const a=m.data,e=m.offset;for(let s=0;s<l;s++)c[s]=Math.round(Number(a[e+s]))}else for(let a=0;a<l;a++)c[a]=Math.round(Number(m.iget(a)));return u}function Bt(t,r){if(S(t.dtype,"ldexp","ldexp is not defined for complex numbers."),typeof r!="number"&&S(r.dtype,"ldexp","ldexp is not defined for complex numbers."),typeof r=="number"){const f=b.zeros(Array.from(t.shape),"float64"),i=f.data,m=t.size,u=Math.pow(2,r);for(let c=0;c<m;c++)i[c]=Number(t.iget(c))*u;return f}return D(t,r,(f,i)=>f*Math.pow(2,i),"ldexp")}function Dt(t){S(t.dtype,"modf","modf is not defined for complex numbers.");const r=b.zeros(Array.from(t.shape),"float64"),f=b.zeros(Array.from(t.shape),"float64"),i=r.data,m=f.data,u=t.size;for(let c=0;c<u;c++){const l=Number(t.iget(c)),a=Math.trunc(l);m[c]=a,i[c]=l-a}return[r,f]}function Ft(t,r,f){S(t.dtype,"clip","clip is not supported for complex numbers.");const i=t.dtype,m=Array.from(t.shape),u=t.size,c=b.zeros(m,i),l=c.data,a=r===null||typeof r=="number",e=f===null||typeof f=="number",s=r===null?-1/0:typeof r=="number"?r:null,o=f===null?1/0:typeof f=="number"?f:null,n=t.isCContiguous,y=t.data,p=t.offset,d=!a&&r.isCContiguous,A=a?null:r.data,g=a?0:r.offset,h=a?0:r.size,C=!e&&f.isCContiguous,w=e?null:f.data,v=e?0:f.offset,U=e?0:f.size;if(N(i)){const I=l;if(n&&(a||d)&&(e||C)){const z=y,B=A,V=w;for(let O=0;O<u;O++){let R=z[p+O];const L=a?s===-1/0?R:BigInt(Math.round(s)):B[g+O%h],E=e?o===1/0?R:BigInt(Math.round(o)):V[v+O%U];R<L&&(R=L),R>E&&(R=E),I[O]=R}}else for(let z=0;z<u;z++){let B=t.iget(z);const V=a?s===-1/0?B:BigInt(Math.round(s)):r.iget(z%r.size),O=e?o===1/0?B:BigInt(Math.round(o)):f.iget(z%f.size);B<V&&(B=V),B>O&&(B=O),I[z]=B}}else if(n&&(a||d)&&(e||C))for(let I=0;I<u;I++){let z=Number(y[p+I]);const B=a?s:Number(A[g+I%h]),V=e?o:Number(w[v+I%U]);z<B&&(z=B),z>V&&(z=V),l[I]=z}else for(let I=0;I<u;I++){let z=Number(t.iget(I));const B=a?s:Number(r.iget(I%r.size)),V=e?o:Number(f.iget(I%f.size));z<B&&(z=B),z>V&&(z=V),l[I]=z}return c}function Ct(t,r){if(S(t.dtype,"maximum","maximum is not supported for complex numbers."),typeof r!="number"&&S(r.dtype,"maximum","maximum is not supported for complex numbers."),typeof r=="number"){const f=t.dtype,i=Array.from(t.shape),m=t.size,u=b.zeros(i,f),c=u.data,l=t.data;if(N(f)){const a=c,e=l,s=BigInt(Math.round(r));for(let o=0;o<m;o++)a[o]=e[o]>s?e[o]:s}else for(let a=0;a<m;a++){const e=Number(l[a]);c[a]=isNaN(e)||isNaN(r)?NaN:Math.max(e,r)}return u}return D(t,r,(f,i)=>isNaN(f)||isNaN(i)?NaN:Math.max(f,i),"maximum")}function vt(t,r){if(S(t.dtype,"minimum","minimum is not supported for complex numbers."),typeof r!="number"&&S(r.dtype,"minimum","minimum is not supported for complex numbers."),typeof r=="number"){const f=t.dtype,i=Array.from(t.shape),m=t.size,u=b.zeros(i,f),c=u.data,l=t.data;if(N(f)){const a=c,e=l,s=BigInt(Math.round(r));for(let o=0;o<m;o++)a[o]=e[o]<s?e[o]:s}else for(let a=0;a<m;a++){const e=Number(l[a]);c[a]=isNaN(e)||isNaN(r)?NaN:Math.min(e,r)}return u}return D(t,r,(f,i)=>isNaN(f)||isNaN(i)?NaN:Math.min(f,i),"minimum")}function Tt(t,r){if(S(t.dtype,"fmax","fmax is not supported for complex numbers."),typeof r!="number"&&S(r.dtype,"fmax","fmax is not supported for complex numbers."),typeof r=="number"){const f=t.dtype,i=Array.from(t.shape),m=t.size,u=b.zeros(i,f),c=u.data,l=t.data;if(N(f)){const a=c,e=l,s=BigInt(Math.round(r));for(let o=0;o<m;o++)a[o]=e[o]>s?e[o]:s}else for(let a=0;a<m;a++){const e=Number(l[a]);isNaN(e)?c[a]=r:isNaN(r)?c[a]=e:c[a]=Math.max(e,r)}return u}return D(t,r,(f,i)=>isNaN(f)?i:isNaN(i)?f:Math.max(f,i),"fmax")}function Ut(t,r){if(S(t.dtype,"fmin","fmin is not supported for complex numbers."),typeof r!="number"&&S(r.dtype,"fmin","fmin is not supported for complex numbers."),typeof r=="number"){const f=t.dtype,i=Array.from(t.shape),m=t.size,u=b.zeros(i,f),c=u.data,l=t.data;if(N(f)){const a=c,e=l,s=BigInt(Math.round(r));for(let o=0;o<m;o++)a[o]=e[o]<s?e[o]:s}else for(let a=0;a<m;a++){const e=Number(l[a]);isNaN(e)?c[a]=r:isNaN(r)?c[a]=e:c[a]=Math.min(e,r)}return u}return D(t,r,(f,i)=>isNaN(f)?i:isNaN(i)?f:Math.min(f,i),"fmin")}function Vt(t,r=0,f,i){S(t.dtype,"nan_to_num","nan_to_num is not supported for complex numbers.");const m=t.dtype,u=Array.from(t.shape),c=t.size,l=f!==void 0?f:Number.MAX_VALUE,a=i!==void 0?i:-Number.MAX_VALUE,e=b.zeros(u,m),s=e.data,o=t.data;if(N(m)){const n=s,y=o;for(let p=0;p<c;p++)n[p]=y[p]}else for(let n=0;n<c;n++){const y=Number(o[n]);isNaN(y)?s[n]=r:y===1/0?s[n]=l:y===-1/0?s[n]=a:s[n]=y}return e}function wt(t,r,f,i,m){S(t.dtype,"interp","interp is not supported for complex numbers."),S(r.dtype,"interp","interp is not supported for complex numbers."),S(f.dtype,"interp","interp is not supported for complex numbers.");const u=Array.from(t.shape),c=t.size,l=b.zeros(u,"float64"),a=l.data,e=t.data,s=r.data,o=f.data,n=r.size,y=i!==void 0?i:Number(o[0]),p=m!==void 0?m:Number(o[n-1]);for(let d=0;d<c;d++){const A=Number(e[d]);if(A<=Number(s[0])){a[d]=y;continue}if(A>=Number(s[n-1])){a[d]=p;continue}let g=0,h=n-1;for(;h-g>1;){const z=Math.floor((g+h)/2);Number(s[z])<=A?g=z:h=z}const C=Number(s[g]),w=Number(s[h]),v=Number(o[g]),U=Number(o[h]),I=(A-C)/(w-C);a[d]=v+I*(U-v)}return l}function Ot(t,r=Math.PI,f=-1,i=2*Math.PI){S(t.dtype,"unwrap","unwrap is not supported for complex numbers.");const m=Array.from(t.shape),u=m.length;if(f<0&&(f+=u),f<0||f>=u)throw new Error(`axis ${f} is out of bounds for array of dimension ${u}`);if(u===1){const e=t.size,s=b.zeros(m,"float64"),o=s.data,n=t.data;if(e===0)return s;o[0]=Number(n[0]);let y=0;for(let p=1;p<e;p++){const d=Number(n[p-1]),A=Number(n[p]);let g=A-d;g=(g+i/2)%i-i/2,g===-i/2&&A-d>0&&(g=i/2),Math.abs(g)>r&&(y-=Math.round((A-d-g)/i)*i),o[p]=A+y}return s}const c=b.zeros(m,"float64"),l=c.data,a=t.data;for(let e=0;e<t.size;e++)l[e]=Number(a[e]);if(u===2){const[e,s]=m;if(f===0)for(let o=0;o<s;o++){let n=0;for(let y=1;y<e;y++){const p=(y-1)*s+o,d=y*s+o,A=l[p],g=l[d];let h=g-A;h=(h+i/2)%i-i/2,h===-i/2&&g-A>0&&(h=i/2),Math.abs(h)>r&&(n-=Math.round((g-A-h)/i)*i),l[d]=g+n}}else for(let o=0;o<e;o++){let n=0;for(let y=1;y<s;y++){const p=o*s+(y-1),d=o*s+y,A=l[p],g=l[d];let h=g-A;h=(h+i/2)%i-i/2,h===-i/2&&g-A>0&&(h=i/2),Math.abs(h)>r&&(n-=Math.round((g-A-h)/i)*i),l[d]=g+n}}}return c}function Rt(t){S(t.dtype,"sinc","sinc is not supported for complex numbers.");const r=Array.from(t.shape),f=t.size,i=b.zeros(r,"float64"),m=i.data,u=t.data;for(let c=0;c<f;c++){const l=Number(u[c]);if(l===0)m[c]=1;else{const a=Math.PI*l;m[c]=Math.sin(a)/a}}return i}function qt(t){S(t.dtype,"i0","i0 is not supported for complex numbers.");const r=Array.from(t.shape),f=t.size,i=b.zeros(r,"float64"),m=i.data,u=t.data;for(let c=0;c<f;c++){const l=Math.abs(Number(u[c]));m[c]=_(l)}return i}function _(t){const r=Math.abs(t);if(r<3.75){const f=t/3.75,i=f*f;return 1+i*(3.5156229+i*(3.0899424+i*(1.2067492+i*(.2659732+i*(.0360768+i*.0045813)))))}else{const f=3.75/r;return Math.exp(r)/Math.sqrt(r)*(.39894228+f*(.01328592+f*(.00225319+f*(-.00157565+f*(.00916281+f*(-.02057706+f*(.02635537+f*(-.01647633+f*.00392377))))))))}}export{ut as absolute,at as add,pt as cbrt,Ft as clip,ft as divide,gt as divmod,dt as fabs,St as float_power,Y as floorDivide,Tt as fmax,Ut as fmin,Nt as fmod,zt as frexp,Mt as gcd,ht as heaviside,qt as i0,wt as interp,It as lcm,Bt as ldexp,Ct as maximum,vt as minimum,$ as mod,Dt as modf,it as multiply,Vt as nan_to_num,ct as negative,yt as positive,mt as reciprocal,bt as remainder,lt as sign,Rt as sinc,At as square,nt as subtract,Ot as unwrap};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as C}from"../storage";import{isBigIntDType as z,isIntegerDType as L,promoteDTypes as F}from"../dtype";import{elementwiseBinaryOp as R}from"../internal/compute";function _(t,r){if(!L(t)&&t!=="bool")throw new TypeError(`ufunc '${r}' not supported for the input types, and the inputs could not be safely coerced to any supported types`)}function E(t,r){return t.isCContiguous&&r.isCContiguous&&t.shape.length===r.shape.length&&t.shape.every((c,l)=>c===r.shape[l])}function ut(t,r){return _(t.dtype,"bitwise_and"),typeof r=="number"?W(t,r):(_(r.dtype,"bitwise_and"),E(t,r)?Q(t,r):R(t,r,(c,l)=>c&l,"bitwise_and"))}function Q(t,r){const c=F(t.dtype,r.dtype),l=C.zeros(Array.from(t.shape),c),f=t.size,i=t.data,s=r.data,d=t.offset,u=r.offset,o=l.data;if(z(c)){const e=o;if(!z(t.dtype)||!z(r.dtype))for(let n=0;n<f;n++){const a=typeof i[d+n]=="bigint"?i[d+n]:BigInt(Math.round(Number(i[d+n]))),g=typeof s[u+n]=="bigint"?s[u+n]:BigInt(Math.round(Number(s[u+n])));e[n]=a&g}else{const n=i,a=s;for(let g=0;g<f;g++)e[g]=n[d+g]&a[u+g]}}else for(let e=0;e<f;e++)o[e]=i[d+e]&s[u+e];return l}function W(t,r){const c=t.dtype,l=Array.from(t.shape),f=t.data,i=t.offset,s=t.size,d=t.isCContiguous,u=C.zeros(l,c),o=u.data;if(z(c)){const e=o,y=BigInt(Math.round(r));if(d){const n=f;if(i===0)for(let a=0;a<s;a++)e[a]=n[a]&y;else for(let a=0;a<s;a++)e[a]=n[i+a]&y}else for(let n=0;n<s;n++)e[n]=t.iget(n)&y}else if(d)if(i===0)for(let e=0;e<s;e++)o[e]=f[e]&r;else for(let e=0;e<s;e++)o[e]=f[i+e]&r;else for(let e=0;e<s;e++)o[e]=Number(t.iget(e))&r;return u}function ct(t,r){return _(t.dtype,"bitwise_or"),typeof r=="number"?Z(t,r):(_(r.dtype,"bitwise_or"),E(t,r)?Y(t,r):R(t,r,(c,l)=>c|l,"bitwise_or"))}function Y(t,r){const c=F(t.dtype,r.dtype),l=C.zeros(Array.from(t.shape),c),f=t.size,i=t.data,s=r.data,d=t.offset,u=r.offset,o=l.data;if(z(c)){const e=o;if(!z(t.dtype)||!z(r.dtype))for(let n=0;n<f;n++){const a=typeof i[d+n]=="bigint"?i[d+n]:BigInt(Math.round(Number(i[d+n]))),g=typeof s[u+n]=="bigint"?s[u+n]:BigInt(Math.round(Number(s[u+n])));e[n]=a|g}else{const n=i,a=s;for(let g=0;g<f;g++)e[g]=n[d+g]|a[u+g]}}else for(let e=0;e<f;e++)o[e]=i[d+e]|s[u+e];return l}function Z(t,r){const c=t.dtype,l=Array.from(t.shape),f=t.data,i=t.offset,s=t.size,d=t.isCContiguous,u=C.zeros(l,c),o=u.data;if(z(c)){const e=o,y=BigInt(Math.round(r));if(d){const n=f;if(i===0)for(let a=0;a<s;a++)e[a]=n[a]|y;else for(let a=0;a<s;a++)e[a]=n[i+a]|y}else for(let n=0;n<s;n++)e[n]=t.iget(n)|y}else if(d)if(i===0)for(let e=0;e<s;e++)o[e]=f[e]|r;else for(let e=0;e<s;e++)o[e]=f[i+e]|r;else for(let e=0;e<s;e++)o[e]=Number(t.iget(e))|r;return u}function yt(t,r){return _(t.dtype,"bitwise_xor"),typeof r=="number"?tt(t,r):(_(r.dtype,"bitwise_xor"),E(t,r)?x(t,r):R(t,r,(c,l)=>c^l,"bitwise_xor"))}function x(t,r){const c=F(t.dtype,r.dtype),l=C.zeros(Array.from(t.shape),c),f=t.size,i=t.data,s=r.data,d=t.offset,u=r.offset,o=l.data;if(z(c)){const e=o;if(!z(t.dtype)||!z(r.dtype))for(let n=0;n<f;n++){const a=typeof i[d+n]=="bigint"?i[d+n]:BigInt(Math.round(Number(i[d+n]))),g=typeof s[u+n]=="bigint"?s[u+n]:BigInt(Math.round(Number(s[u+n])));e[n]=a^g}else{const n=i,a=s;for(let g=0;g<f;g++)e[g]=n[d+g]^a[u+g]}}else for(let e=0;e<f;e++)o[e]=i[d+e]^s[u+e];return l}function tt(t,r){const c=t.dtype,l=Array.from(t.shape),f=t.data,i=t.offset,s=t.size,d=t.isCContiguous,u=C.zeros(l,c),o=u.data;if(z(c)){const e=o,y=BigInt(Math.round(r));if(d){const n=f;if(i===0)for(let a=0;a<s;a++)e[a]=n[a]^y;else for(let a=0;a<s;a++)e[a]=n[i+a]^y}else for(let n=0;n<s;n++)e[n]=t.iget(n)^y}else if(d)if(i===0)for(let e=0;e<s;e++)o[e]=f[e]^r;else for(let e=0;e<s;e++)o[e]=f[i+e]^r;else for(let e=0;e<s;e++)o[e]=Number(t.iget(e))^r;return u}function H(t){_(t.dtype,"bitwise_not");const r=t.dtype,c=Array.from(t.shape),l=t.data,f=t.offset,i=t.size,s=t.isCContiguous,d=C.zeros(c,r),u=d.data;if(z(r)){const o=u;if(s){const e=l;if(f===0)for(let y=0;y<i;y++)o[y]=~e[y];else for(let y=0;y<i;y++)o[y]=~e[f+y]}else for(let e=0;e<i;e++)o[e]=~t.iget(e)}else if(s)if(f===0)for(let o=0;o<i;o++)u[o]=~l[o];else for(let o=0;o<i;o++)u[o]=~l[f+o];else for(let o=0;o<i;o++)u[o]=~Number(t.iget(o));return d}function lt(t){return H(t)}function et(t,r){if(_(t.dtype,"left_shift"),typeof r=="number")return J(t,r);if(_(r.dtype,"left_shift"),r.size===1||r.ndim===1&&r.shape[0]===1){const c=Number(r.iget(0));return J(t,c)}return E(t,r)?rt(t,r):R(t,r,(c,l)=>c<<l,"left_shift")}function rt(t,r){const c=F(t.dtype,r.dtype),l=C.zeros(Array.from(t.shape),c),f=t.size,i=t.data,s=r.data,d=t.offset,u=r.offset,o=l.data;if(z(c)){const e=o;for(let y=0;y<f;y++){const n=typeof i[d+y]=="bigint"?i[d+y]:BigInt(Math.round(Number(i[d+y]))),a=typeof s[u+y]=="bigint"?s[u+y]:BigInt(Math.round(Number(s[u+y])));e[y]=n<<a}}else for(let e=0;e<f;e++)o[e]=i[d+e]<<s[u+e];return l}function J(t,r){const c=t.dtype,l=Array.from(t.shape),f=t.data,i=t.offset,s=t.size,d=t.isCContiguous,u=C.zeros(l,c),o=u.data;if(z(c)){const e=o,y=BigInt(Math.round(r));if(d){const n=f;if(i===0)for(let a=0;a<s;a++)e[a]=n[a]<<y;else for(let a=0;a<s;a++)e[a]=n[i+a]<<y}else for(let n=0;n<s;n++)e[n]=t.iget(n)<<y}else if(d)if(i===0)for(let e=0;e<s;e++)o[e]=f[e]<<r;else for(let e=0;e<s;e++)o[e]=f[i+e]<<r;else for(let e=0;e<s;e++)o[e]=Number(t.iget(e))<<r;return u}function ot(t,r){if(_(t.dtype,"right_shift"),typeof r=="number")return K(t,r);if(_(r.dtype,"right_shift"),r.size===1||r.ndim===1&&r.shape[0]===1){const c=Number(r.iget(0));return K(t,c)}return E(t,r)?nt(t,r):R(t,r,(c,l)=>c>>l,"right_shift")}function nt(t,r){const c=F(t.dtype,r.dtype),l=C.zeros(Array.from(t.shape),c),f=t.size,i=t.data,s=r.data,d=t.offset,u=r.offset,o=l.data;if(z(c)){const e=o;for(let y=0;y<f;y++){const n=typeof i[d+y]=="bigint"?i[d+y]:BigInt(Math.round(Number(i[d+y]))),a=typeof s[u+y]=="bigint"?s[u+y]:BigInt(Math.round(Number(s[u+y])));e[y]=n>>a}}else for(let e=0;e<f;e++)o[e]=i[d+e]>>s[u+e];return l}function K(t,r){const c=t.dtype,l=Array.from(t.shape),f=t.data,i=t.offset,s=t.size,d=t.isCContiguous,u=C.zeros(l,c),o=u.data;if(z(c)){const e=o,y=BigInt(Math.round(r));if(d){const n=f;if(i===0)for(let a=0;a<s;a++)e[a]=n[a]>>y;else for(let a=0;a<s;a++)e[a]=n[i+a]>>y}else for(let n=0;n<s;n++)e[n]=t.iget(n)>>y}else if(d)if(i===0)for(let e=0;e<s;e++)o[e]=f[e]>>r;else for(let e=0;e<s;e++)o[e]=f[i+e]>>r;else for(let e=0;e<s;e++)o[e]=Number(t.iget(e))>>r;return u}function dt(t,r=-1,c="big"){const l=Array.from(t.shape),f=l.length;if(r<0&&(r=f+r),r<0||r>=f)throw new Error(`axis ${r} is out of bounds for array of dimension ${f}`);const i=l[r],s=Math.ceil(i/8),d=[...l];d[r]=s;const u=C.zeros(d,"uint8"),o=u.data,e=t.offset,y=t.isCContiguous;if(f===1){for(let I=0;I<s;I++){let p=0;for(let b=0;b<8;b++){const S=I*8+b;if(S<i){const w=y?Number(t.data[e+S])!==0?1:0:Number(t.iget(S))!==0?1:0;c==="big"?p|=w<<7-b:p|=w<<b}}o[I]=p}return u}const n=l.slice(0,r),a=l.slice(r+1),g=n.reduce((I,p)=>I*p,1),X=a.reduce((I,p)=>I*p,1),k=$(l),v=$(d);for(let I=0;I<g;I++)for(let p=0;p<X;p++)for(let b=0;b<s;b++){let S=0;for(let h=0;h<8;h++){const A=b*8+h;if(A<i){let B=0,D=I;for(let T=0;T<r;T++){const N=T<r-1?n.slice(T+1).reduce((P,j)=>P*j,1):1,V=Math.floor(D/N);D%=N,B+=V*k[T]}B+=A*k[r];let m=p;for(let T=r+1;T<f;T++){const N=T<f-1?a.slice(T-r).reduce((P,j)=>P*j,1):1,V=Math.floor(m/N);m%=N,B+=V*k[T]}const U=y?Number(t.data[e+B])!==0?1:0:Number(t.iget(B))!==0?1:0;c==="big"?S|=U<<7-h:S|=U<<h}}let w=0,M=I;for(let h=0;h<r;h++){const A=h<r-1?n.slice(h+1).reduce((D,m)=>D*m,1):1,B=Math.floor(M/A);M%=A,w+=B*v[h]}w+=b*v[r];let O=p;for(let h=r+1;h<f;h++){const A=h<f-1?a.slice(h-r).reduce((D,m)=>D*m,1):1,B=Math.floor(O/A);O%=A,w+=B*v[h]}o[w]=S}return u}function gt(t,r=-1,c=-1,l="big"){if(t.dtype!=="uint8")throw new TypeError("Expected an input array of unsigned byte data type");const f=Array.from(t.shape),i=f.length;if(r<0&&(r=i+r),r<0||r>=i)throw new Error(`axis ${r} is out of bounds for array of dimension ${i}`);const s=f[r];let d=s*8;c>=0&&(d=c);const u=[...f];u[r]=d;const o=C.zeros(u,"uint8"),e=o.data,y=t.offset,n=t.isCContiguous;if(i===1){for(let p=0;p<s;p++){const b=Number(n?t.data[y+p]:t.iget(p));for(let S=0;S<8;S++){const w=p*8+S;if(w>=d)break;l==="big"?e[w]=b>>7-S&1:e[w]=b>>S&1}}return o}const a=f.slice(0,r),g=f.slice(r+1),X=a.reduce((p,b)=>p*b,1),k=g.reduce((p,b)=>p*b,1),v=$(f),I=$(u);for(let p=0;p<X;p++)for(let b=0;b<k;b++)for(let S=0;S<s;S++){let w=0,M=p;for(let A=0;A<r;A++){const B=A<r-1?a.slice(A+1).reduce((m,U)=>m*U,1):1,D=Math.floor(M/B);M%=B,w+=D*v[A]}w+=S*v[r];let O=b;for(let A=r+1;A<i;A++){const B=A<i-1?g.slice(A-r).reduce((m,U)=>m*U,1):1,D=Math.floor(O/B);O%=B,w+=D*v[A]}const h=Number(n?t.data[y+w]:t.iget(w));for(let A=0;A<8;A++){const B=S*8+A;if(B>=d)break;let D=0;M=p;for(let m=0;m<r;m++){const U=m<r-1?a.slice(m+1).reduce((N,V)=>N*V,1):1,T=Math.floor(M/U);M%=U,D+=T*I[m]}D+=B*I[r],O=b;for(let m=r+1;m<i;m++){const U=m<i-1?g.slice(m-r).reduce((N,V)=>N*V,1):1,T=Math.floor(O/U);O%=U,D+=T*I[m]}l==="big"?e[D]=h>>7-A&1:e[D]=h>>A&1}}return o}function $(t){const r=t.length,c=new Array(r);let l=1;for(let f=r-1;f>=0;f--)c[f]=l,l*=t[f];return c}function pt(t){const r=t.dtype;_(r,"bitwise_count");const c=Array.from(t.shape),l=t.data,f=t.offset,i=t.size,s=t.isCContiguous,d=C.zeros(c,"uint8"),u=d.data;if(z(r))if(s){const o=l;if(f===0)for(let e=0;e<i;e++)u[e]=G(o[e]);else for(let e=0;e<i;e++)u[e]=G(o[f+e])}else for(let o=0;o<i;o++)u[o]=G(t.iget(o));else if(s)if(f===0)for(let o=0;o<i;o++)u[o]=q(l[o]);else for(let o=0;o<i;o++)u[o]=q(l[f+o]);else for(let o=0;o<i;o++)u[o]=q(Number(t.iget(o)));return d}function q(t){t=t>>>0;let r=0;for(;t!==0;)t=t&t-1,r++;return r}function G(t){t<0n&&(t=BigInt.asUintN(64,t));let r=0;for(;t!==0n;)t=t&t-1n,r++;return r}function At(t){return H(t)}function bt(t,r){return et(t,r)}function mt(t,r){return ot(t,r)}export{ut as bitwise_and,pt as bitwise_count,At as bitwise_invert,bt as bitwise_left_shift,H as bitwise_not,ct as bitwise_or,mt as bitwise_right_shift,yt as bitwise_xor,lt as invert,et as left_shift,dt as packbits,ot as right_shift,gt as unpackbits};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as y}from"../storage";import{isBigIntDType as z,isComplexDType as c}from"../dtype";import{elementwiseComparisonOp as p}from"../internal/compute";import{computeBroadcastShape as g,broadcastTo as D}from"../broadcasting";import{Complex as v}from"../complex";function d(r,t){return[r[t*2],r[t*2+1]]}function x(r,t){if(c(r.dtype))return d(r.data,t);const o=r.iget(t);return o instanceof v?[o.re,o.im]:[Number(o),0]}function S(r,t,o){const i=g([Array.from(r.shape),Array.from(t.shape)]);if(!i)throw new Error("Cannot broadcast arrays together");const u=D(r,i),e=D(t,i),n=i.reduce((a,s)=>a*s,1),f=new Uint8Array(n);for(let a=0;a<n;a++){const[s,l]=x(u,a),[m,b]=x(e,a);f[a]=o(s,l,m,b)?1:0}return y.fromData(f,i,"bool")}function H(r,t){return typeof t=="number"?B(r,t):c(r.dtype)||c(t.dtype)?S(r,t,(o,i,u,e)=>o!==u?o>u:i>e):p(r,t,(o,i)=>o>i)}function J(r,t){return typeof t=="number"?I(r,t):c(r.dtype)||c(t.dtype)?S(r,t,(o,i,u,e)=>o!==u?o>=u:i>=e):p(r,t,(o,i)=>o>=i)}function K(r,t){return typeof t=="number"?N(r,t):c(r.dtype)||c(t.dtype)?S(r,t,(o,i,u,e)=>o!==u?o<u:i<e):p(r,t,(o,i)=>o<i)}function L(r,t){return typeof t=="number"?U(r,t):c(r.dtype)||c(t.dtype)?S(r,t,(o,i,u,e)=>o!==u?o<=u:i<=e):p(r,t,(o,i)=>o<=i)}function P(r,t){return typeof t=="number"?q(r,t):c(r.dtype)||c(t.dtype)?S(r,t,(o,i,u,e)=>o===u&&i===e):p(r,t,(o,i)=>o===i)}function Q(r,t){return typeof t=="number"?E(r,t):c(r.dtype)||c(t.dtype)?S(r,t,(o,i,u,e)=>o!==u||i!==e):p(r,t,(o,i)=>o!==i)}function w(r,t,o=1e-5,i=1e-8){return typeof t=="number"?M(r,t,o,i):p(r,t,(u,e)=>{const n=Math.abs(u-e),f=i+o*Math.abs(e);return n<=f})}function V(r,t,o=1e-5,i=1e-8){const u=w(r,t,o,i),e=u.data;for(let n=0;n<u.size;n++)if(e[n]===0)return!1;return!0}function W(r,t){const o=[Array.from(r.shape),Array.from(t.shape)],i=g(o);if(i===null)return!1;const u=D(r,i),e=D(t,i),n=i.length,f=i.reduce((l,m)=>l*m,1),a=z(u.dtype),s=z(e.dtype);for(let l=0;l<f;l++){let m=l;const b=new Array(n);for(let A=n-1;A>=0;A--)b[A]=m%i[A],m=Math.floor(m/i[A]);const h=u.get(...b),C=e.get(...b);if(a||s){const A=typeof h=="bigint"?h:BigInt(Number(h)),F=typeof C=="bigint"?C:BigInt(Number(C));if(A!==F)return!1}else if(h!==C)return!1}return!0}function B(r,t){const o=new Uint8Array(r.size),i=r.size,u=r.isCContiguous;if(c(r.dtype))if(u){const e=r.data,n=r.offset;for(let f=0;f<i;f++){const[a,s]=d(e,n+f);o[f]=(a!==t?a>t:s>0)?1:0}}else for(let e=0;e<i;e++){const n=r.iget(e);o[e]=(n.re!==t?n.re>t:n.im>0)?1:0}else if(u){const e=r.data,n=r.offset;for(let f=0;f<i;f++)o[f]=e[n+f]>t?1:0}else for(let e=0;e<i;e++)o[e]=Number(r.iget(e))>t?1:0;return y.fromData(o,Array.from(r.shape),"bool")}function I(r,t){const o=new Uint8Array(r.size),i=r.size,u=r.isCContiguous;if(c(r.dtype))if(u){const e=r.data,n=r.offset;for(let f=0;f<i;f++){const[a,s]=d(e,n+f);o[f]=(a!==t?a>=t:s>=0)?1:0}}else for(let e=0;e<i;e++){const n=r.iget(e);o[e]=(n.re!==t?n.re>=t:n.im>=0)?1:0}else if(u){const e=r.data,n=r.offset;for(let f=0;f<i;f++)o[f]=e[n+f]>=t?1:0}else for(let e=0;e<i;e++)o[e]=Number(r.iget(e))>=t?1:0;return y.fromData(o,Array.from(r.shape),"bool")}function N(r,t){const o=new Uint8Array(r.size),i=r.size,u=r.isCContiguous;if(c(r.dtype))if(u){const e=r.data,n=r.offset;for(let f=0;f<i;f++){const[a,s]=d(e,n+f);o[f]=(a!==t?a<t:s<0)?1:0}}else for(let e=0;e<i;e++){const n=r.iget(e);o[e]=(n.re!==t?n.re<t:n.im<0)?1:0}else if(u){const e=r.data,n=r.offset;for(let f=0;f<i;f++)o[f]=e[n+f]<t?1:0}else for(let e=0;e<i;e++)o[e]=Number(r.iget(e))<t?1:0;return y.fromData(o,Array.from(r.shape),"bool")}function U(r,t){const o=new Uint8Array(r.size),i=r.size,u=r.isCContiguous;if(c(r.dtype))if(u){const e=r.data,n=r.offset;for(let f=0;f<i;f++){const[a,s]=d(e,n+f);o[f]=(a!==t?a<=t:s<=0)?1:0}}else for(let e=0;e<i;e++){const n=r.iget(e);o[e]=(n.re!==t?n.re<=t:n.im<=0)?1:0}else if(u){const e=r.data,n=r.offset;for(let f=0;f<i;f++)o[f]=e[n+f]<=t?1:0}else for(let e=0;e<i;e++)o[e]=Number(r.iget(e))<=t?1:0;return y.fromData(o,Array.from(r.shape),"bool")}function q(r,t){const o=new Uint8Array(r.size),i=r.size,u=r.dtype,e=r.isCContiguous;if(c(u))if(e){const n=r.data,f=r.offset;for(let a=0;a<i;a++){const[s,l]=d(n,f+a);o[a]=s===t&&l===0?1:0}}else for(let n=0;n<i;n++){const f=r.iget(n);o[n]=f.re===t&&f.im===0?1:0}else if(z(u)){const n=BigInt(Math.round(t));if(e){const f=r.data,a=r.offset;for(let s=0;s<i;s++)o[s]=f[a+s]===n?1:0}else for(let f=0;f<i;f++)o[f]=r.iget(f)===n?1:0}else if(e){const n=r.data,f=r.offset;for(let a=0;a<i;a++)o[a]=n[f+a]===t?1:0}else for(let n=0;n<i;n++)o[n]=Number(r.iget(n))===t?1:0;return y.fromData(o,Array.from(r.shape),"bool")}function E(r,t){const o=new Uint8Array(r.size),i=r.size,u=r.dtype,e=r.isCContiguous;if(c(u))if(e){const n=r.data,f=r.offset;for(let a=0;a<i;a++){const[s,l]=d(n,f+a);o[a]=s!==t||l!==0?1:0}}else for(let n=0;n<i;n++){const f=r.iget(n);o[n]=f.re!==t||f.im!==0?1:0}else if(e){const n=r.data,f=r.offset;for(let a=0;a<i;a++)o[a]=n[f+a]!==t?1:0}else for(let n=0;n<i;n++)o[n]=Number(r.iget(n))!==t?1:0;return y.fromData(o,Array.from(r.shape),"bool")}function M(r,t,o,i){const u=new Uint8Array(r.size),e=r.size,n=i+o*Math.abs(t);if(r.isCContiguous){const f=r.data,a=r.offset;for(let s=0;s<e;s++){const l=Math.abs(Number(f[a+s])-t);u[s]=l<=n?1:0}}else for(let f=0;f<e;f++){const a=Math.abs(Number(r.iget(f))-t);u[f]=a<=n?1:0}return y.fromData(u,Array.from(r.shape),"bool")}export{V as allclose,W as arrayEquiv,P as equal,H as greater,J as greaterEqual,w as isclose,K as less,L as lessEqual,Q as notEqual};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as i}from"../storage";import{isComplexDType as A,getComplexComponentDType as u,getTypedArrayConstructor as m}from"../dtype";import{Complex as D}from"../complex";function z(r){const a=r.dtype,n=Array.from(r.shape),y=r.size;if(A(a)){const c=u(a),s=i.zeros(n,c),e=s.data,o=r.data;for(let t=0;t<y;t++)e[t]=o[t*2];return s}return r.copy()}function S(r){const a=r.dtype,n=Array.from(r.shape),y=r.size;if(A(a)){const s=u(a),e=i.zeros(n,s),o=e.data,t=r.data;for(let l=0;l<y;l++)o[l]=t[l*2+1];return e}const c=a==="float32"?"float32":"float64";return i.zeros(n,c)}function F(r){const a=r.dtype,n=Array.from(r.shape),y=r.size;if(A(a)){const c=m(a),s=y*2,e=new c(s),o=r.data;for(let t=0;t<y;t++)e[t*2]=o[t*2],e[t*2+1]=-o[t*2+1];return i.fromData(e,n,a)}return r.copy()}const x=F;function T(r,a=!1){const n=r.dtype,y=Array.from(r.shape),c=r.size,s=i.zeros(y,"float64"),e=s.data;if(A(n)){const o=r.data;for(let t=0;t<c;t++){const l=o[t*2],p=o[t*2+1];let f=Math.atan2(p,l);a&&(f=f*180/Math.PI),e[t]=f}}else for(let o=0;o<c;o++){const t=r.iget(o);let p=(t instanceof D?t.re:Number(t))>=0?0:Math.PI;a&&(p=p*180/Math.PI),e[o]=p}return s}function C(r){const a=r.dtype,n=Array.from(r.shape),y=r.size,c=A(a)?u(a):a,s=i.zeros(n,c),e=s.data;if(A(a)){const o=r.data;for(let t=0;t<y;t++){const l=o[t*2],p=o[t*2+1];e[t]=Math.sqrt(l*l+p*p)}}return s}export{T as angle,C as complexAbs,F as conj,x as conjugate,S as imag,z as real};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as d}from"../storage";import{elementwiseUnaryOp as D,elementwiseBinaryOp as T,broadcastShapes as q}from"../internal/compute";import{isBigIntDType as w,isComplexDType as A,throwIfComplex as N}from"../dtype";function j(t){const r=t.dtype;if(A(r)){const f=Array.from(t.shape),y=t.size,u=t.isCContiguous,l=d.zeros(f,r),p=l.data;if(u){const e=t.data,c=t.offset;for(let a=0;a<y;a++){const o=e[(c+a)*2],s=e[(c+a)*2+1],i=Math.sqrt(o*o+s*s),n=Math.sqrt((i+o)/2),m=(s>=0?1:-1)*Math.sqrt((i-o)/2);p[a*2]=n,p[a*2+1]=m}}else for(let e=0;e<y;e++){const c=t.iget(e),a=c.re,o=c.im,s=Math.sqrt(a*a+o*o),i=Math.sqrt((s+a)/2),n=(o>=0?1:-1)*Math.sqrt((s-a)/2);p[e*2]=i,p[e*2+1]=n}return l}return D(t,Math.sqrt,!1)}function k(t,r){if(typeof r=="number")return V(t,r);const f=A(t.dtype),y=A(r.dtype);return f||y?I(t,r):T(t,r,Math.pow,"power")}function I(t,r){const f=A(t.dtype),y=A(r.dtype),u=t.dtype==="complex128"||r.dtype==="complex128"||r.dtype==="float64"?"complex128":"complex64",l=Array.from(t.shape),p=t.size,e=d.zeros(l,u),c=e.data,a=t.isCContiguous,o=r.isCContiguous,s=t.offset,i=r.offset;for(let n=0;n<p;n++){let m,g;if(f)if(a){const S=t.data;m=S[(s+n)*2],g=S[(s+n)*2+1]}else{const S=t.iget(n);m=S.re,g=S.im}else m=Number(t.iget(n)),g=0;let h,M;if(y)if(o){const S=r.data;h=S[(i+n)*2],M=S[(i+n)*2+1]}else{const S=r.iget(n);h=S.re,M=S.im}else h=Number(r.iget(n)),M=0;const C=Math.sqrt(m*m+g*g),z=Math.atan2(g,m),b=Math.log(C),F=h*b-M*z,x=h*z+M*b,v=Math.exp(F);c[n*2]=v*Math.cos(x),c[n*2+1]=v*Math.sin(x)}return e}function V(t,r){const f=t.dtype,y=Array.from(t.shape),u=t.data,l=t.size,p=t.offset,e=t.isCContiguous;if(A(f)){const n=d.zeros(y,f),m=n.data;if(e){const g=u;for(let h=0;h<l;h++){const M=g[(p+h)*2],C=g[(p+h)*2+1],z=Math.sqrt(M*M+C*C),b=Math.atan2(C,M),F=Math.pow(z,r),x=b*r;m[h*2]=F*Math.cos(x),m[h*2+1]=F*Math.sin(x)}}else for(let g=0;g<l;g++){const h=t.iget(g),M=h.re,C=h.im,z=Math.sqrt(M*M+C*C),b=Math.atan2(C,M),F=Math.pow(z,r),x=b*r;m[g*2]=F*Math.cos(x),m[g*2+1]=F*Math.sin(x)}return n}const o=f!=="float32"&&f!=="float64"&&(r<0||!Number.isInteger(r))?"float64":f,s=d.zeros(y,o),i=s.data;if(w(f))if(w(o)&&Number.isInteger(r)&&r>=0)if(e){const n=u,m=i;for(let g=0;g<l;g++)m[g]=n[p+g]**BigInt(r)}else{const n=i;for(let m=0;m<l;m++)n[m]=t.iget(m)**BigInt(r)}else if(e)for(let n=0;n<l;n++)i[n]=Math.pow(Number(u[p+n]),r);else for(let n=0;n<l;n++)i[n]=Math.pow(Number(t.iget(n)),r);else if(e)for(let n=0;n<l;n++)i[n]=Math.pow(Number(u[p+n]),r);else for(let n=0;n<l;n++)i[n]=Math.pow(Number(t.iget(n)),r);return s}function H(t){const r=t.dtype;if(A(r)){const f=Array.from(t.shape),y=t.size,u=t.isCContiguous,l=d.zeros(f,r),p=l.data;if(u){const e=t.data,c=t.offset;for(let a=0;a<y;a++){const o=e[(c+a)*2],s=e[(c+a)*2+1],i=Math.exp(o);p[a*2]=i*Math.cos(s),p[a*2+1]=i*Math.sin(s)}}else for(let e=0;e<y;e++){const c=t.iget(e),a=c.re,o=c.im,s=Math.exp(a);p[e*2]=s*Math.cos(o),p[e*2+1]=s*Math.sin(o)}return l}return D(t,Math.exp,!1)}function J(t){const r=t.dtype;if(A(r)){const f=Array.from(t.shape),y=t.size,u=t.isCContiguous,l=Math.LN2,p=d.zeros(f,r),e=p.data;if(u){const c=t.data,a=t.offset;for(let o=0;o<y;o++){const s=c[(a+o)*2],i=c[(a+o)*2+1],n=Math.exp(s*l),m=i*l;e[o*2]=n*Math.cos(m),e[o*2+1]=n*Math.sin(m)}}else for(let c=0;c<y;c++){const a=t.iget(c),o=a.re,s=a.im,i=Math.exp(o*l),n=s*l;e[c*2]=i*Math.cos(n),e[c*2+1]=i*Math.sin(n)}return p}return D(t,f=>Math.pow(2,f),!1)}function K(t){const r=t.dtype;if(A(r)){const f=Array.from(t.shape),y=t.size,u=t.isCContiguous,l=d.zeros(f,r),p=l.data;if(u){const e=t.data,c=t.offset;for(let a=0;a<y;a++){const o=e[(c+a)*2],s=e[(c+a)*2+1],i=Math.exp(o);p[a*2]=i*Math.cos(s)-1,p[a*2+1]=i*Math.sin(s)}}else for(let e=0;e<y;e++){const c=t.iget(e),a=c.re,o=c.im,s=Math.exp(a);p[e*2]=s*Math.cos(o)-1,p[e*2+1]=s*Math.sin(o)}return l}return D(t,Math.expm1,!1)}function Q(t){const r=t.dtype;if(A(r)){const f=Array.from(t.shape),y=t.size,u=t.isCContiguous,l=d.zeros(f,r),p=l.data;if(u){const e=t.data,c=t.offset;for(let a=0;a<y;a++){const o=e[(c+a)*2],s=e[(c+a)*2+1],i=Math.sqrt(o*o+s*s),n=Math.atan2(s,o);p[a*2]=Math.log(i),p[a*2+1]=n}}else for(let e=0;e<y;e++){const c=t.iget(e),a=c.re,o=c.im,s=Math.sqrt(a*a+o*o),i=Math.atan2(o,a);p[e*2]=Math.log(s),p[e*2+1]=i}return l}return D(t,Math.log,!1)}function W(t){const r=t.dtype;if(A(r)){const f=Array.from(t.shape),y=t.size,u=t.isCContiguous,l=1/Math.LN2,p=d.zeros(f,r),e=p.data;if(u){const c=t.data,a=t.offset;for(let o=0;o<y;o++){const s=c[(a+o)*2],i=c[(a+o)*2+1],n=Math.sqrt(s*s+i*i),m=Math.atan2(i,s);e[o*2]=Math.log(n)*l,e[o*2+1]=m*l}}else for(let c=0;c<y;c++){const a=t.iget(c),o=a.re,s=a.im,i=Math.sqrt(o*o+s*s),n=Math.atan2(s,o);e[c*2]=Math.log(i)*l,e[c*2+1]=n*l}return p}return D(t,Math.log2,!1)}function X(t){const r=t.dtype;if(A(r)){const f=Array.from(t.shape),y=t.size,u=t.isCContiguous,l=1/Math.LN10,p=d.zeros(f,r),e=p.data;if(u){const c=t.data,a=t.offset;for(let o=0;o<y;o++){const s=c[(a+o)*2],i=c[(a+o)*2+1],n=Math.sqrt(s*s+i*i),m=Math.atan2(i,s);e[o*2]=Math.log(n)*l,e[o*2+1]=m*l}}else for(let c=0;c<y;c++){const a=t.iget(c),o=a.re,s=a.im,i=Math.sqrt(o*o+s*s),n=Math.atan2(s,o);e[c*2]=Math.log(i)*l,e[c*2+1]=n*l}return p}return D(t,Math.log10,!1)}function Y(t){const r=t.dtype;if(A(r)){const f=Array.from(t.shape),y=t.size,u=t.isCContiguous,l=d.zeros(f,r),p=l.data;if(u){const e=t.data,c=t.offset;for(let a=0;a<y;a++){const o=e[(c+a)*2],s=e[(c+a)*2+1],i=1+o,n=Math.sqrt(i*i+s*s),m=Math.atan2(s,i);p[a*2]=Math.log(n),p[a*2+1]=m}}else for(let e=0;e<y;e++){const c=t.iget(e),a=c.re,o=c.im,s=1+a,i=Math.sqrt(s*s+o*o),n=Math.atan2(o,s);p[e*2]=Math.log(i),p[e*2+1]=n}return l}return D(t,Math.log1p,!1)}function Z(t,r){return N(t.dtype,"logaddexp","logaddexp is not supported for complex numbers."),typeof r!="number"&&N(r.dtype,"logaddexp","logaddexp is not supported for complex numbers."),typeof r=="number"?B(t,r):R(t,r)}function R(t,r){const f=q(t.shape,r.shape),y=f.reduce((a,o)=>a*o,1),u=t.dtype,l=r.dtype,p=u==="float32"&&l==="float32"?"float32":"float64",e=d.zeros(f,p),c=e.data;for(let a=0;a<y;a++){const o=(w(u),Number(t.iget(a))),s=(w(l),Number(r.iget(a))),i=Math.max(o,s),n=Math.min(o,s);c[a]=i+Math.log1p(Math.exp(n-i))}return e}function B(t,r){const f=t.dtype,y=Array.from(t.shape),u=t.size,l=t.isCContiguous,p=f==="float32"?"float32":"float64",e=d.zeros(y,p),c=e.data;if(l){const a=t.offset;for(let o=0;o<u;o++){const s=Number(t.data[a+o]),i=Math.max(s,r),n=Math.min(s,r);c[o]=i+Math.log1p(Math.exp(n-i))}}else for(let a=0;a<u;a++){const o=Number(t.iget(a)),s=Math.max(o,r),i=Math.min(o,r);c[a]=s+Math.log1p(Math.exp(i-s))}return e}function $(t,r){return N(t.dtype,"logaddexp2","logaddexp2 is not supported for complex numbers."),typeof r!="number"&&N(r.dtype,"logaddexp2","logaddexp2 is not supported for complex numbers."),typeof r=="number"?P(t,r):L(t,r)}function L(t,r){const f=q(t.shape,r.shape),y=f.reduce((o,s)=>o*s,1),u=t.dtype,l=r.dtype,p=u==="float32"&&l==="float32"?"float32":"float64",e=d.zeros(f,p),c=e.data,a=Math.LOG2E;for(let o=0;o<y;o++){const s=(w(u),Number(t.iget(o))),i=(w(l),Number(r.iget(o))),n=Math.max(s,i),m=Math.min(s,i);c[o]=n+Math.log1p(Math.pow(2,m-n))*a}return e}function P(t,r){const f=t.dtype,y=Array.from(t.shape),u=t.size,l=t.isCContiguous,p=f==="float32"?"float32":"float64",e=d.zeros(y,p),c=e.data,a=Math.LOG2E;if(l){const o=t.offset;for(let s=0;s<u;s++){const i=Number(t.data[o+s]),n=Math.max(i,r),m=Math.min(i,r);c[s]=n+Math.log1p(Math.pow(2,m-n))*a}}else for(let o=0;o<u;o++){const s=Number(t.iget(o)),i=Math.max(s,r),n=Math.min(s,r);c[o]=i+Math.log1p(Math.pow(2,n-i))*a}return e}export{H as exp,J as exp2,K as expm1,Q as log,X as log10,Y as log1p,W as log2,Z as logaddexp,$ as logaddexp2,k as power,j as sqrt};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as b}from"../storage";import{Complex as C}from"../complex";function U(o,r,a){const e=o.length;e!==0&&e!==1&&((e&e-1)===0?D(o,r,a):v(o,r,a))}const z=new Map;function K(o,r){const a=`${o}_${r}`;let e=z.get(a);if(e)return e;const f=new Float64Array(o/2),l=new Float64Array(o/2),c=r?1:-1;for(let t=0;t<o/2;t++){const n=c*2*Math.PI*t/o;f[t]=Math.cos(n),l[t]=Math.sin(n)}if(e={cos:f,sin:l},z.set(a,e),z.size>100){const t=z.keys().next().value;z.delete(t)}return e}function D(o,r,a){const e=o.length;let f=0;for(let t=0;t<e-1;t++){if(t<f){let d=o[t];o[t]=o[f],o[f]=d,d=r[t],r[t]=r[f],r[f]=d}let n=e>>1;for(;n<=f;)f-=n,n>>=1;f+=n}const{cos:l,sin:c}=K(e,a);for(let t=2;t<=e;t*=2){const n=t>>1,d=e/t;for(let u=0;u<e;u+=t)for(let i=0,s=0;i<n;i++,s+=d){const y=l[s],m=c[s],h=u+i,A=u+i+n,p=o[h],g=r[h],S=o[A],w=r[A],F=y*S-m*w,_=y*w+m*S;o[h]=p+F,r[h]=g+_,o[A]=p-F,r[A]=g-_}}if(a){const t=1/e;for(let n=0;n<e;n++)o[n]=o[n]*t,r[n]=r[n]*t}}function v(o,r,a){const e=o.length;let f=1;for(;f<2*e-1;)f*=2;const l=a?1:-1,c=new Float64Array(e),t=new Float64Array(e);for(let s=0;s<e;s++){const y=l*Math.PI*s*s/e;c[s]=Math.cos(y),t[s]=Math.sin(y)}const n=new Float64Array(f),d=new Float64Array(f);for(let s=0;s<e;s++){const y=c[s],m=t[s];n[s]=o[s]*y-r[s]*m,d[s]=r[s]*y+o[s]*m}const u=new Float64Array(f),i=new Float64Array(f);u[0]=c[0],i[0]=-t[0];for(let s=1;s<e;s++)u[s]=c[s],i[s]=-t[s],u[f-s]=c[s],i[f-s]=-t[s];D(n,d,!1),D(u,i,!1);for(let s=0;s<f;s++){const y=n[s],m=d[s],h=u[s],A=i[s];n[s]=y*h-m*A,d[s]=y*A+m*h}D(n,d,!0);for(let s=0;s<e;s++){const y=n[s],m=d[s],h=c[s],A=t[s];o[s]=y*h-m*A,r[s]=m*h+y*A}if(a)for(let s=0;s<e;s++)o[s]=o[s]/e,r[s]=r[s]/e}function x(o,r,a=-1,e="backward"){return k(o,r!==void 0?[r]:void 0,[a],e,!1)}function M(o,r,a=-1,e="backward"){return k(o,r!==void 0?[r]:void 0,[a],e,!0)}function G(o,r,a=[-2,-1],e="backward"){return k(o,r,a,e,!1)}function H(o,r,a=[-2,-1],e="backward"){return k(o,r,a,e,!0)}function J(o,r,a,e="backward"){return k(o,r,a,e,!1)}function Q(o,r,a,e="backward"){return k(o,r,a,e,!0)}function k(o,r,a,e="backward",f=!1){const l=Array.from(o.shape),c=l.length;if(o.size===0)return b.zeros(l,"complex128");if(c===0){const u=b.zeros([1],"complex128"),i=o.iget(0),s=i instanceof C?i.re:Number(i),y=i instanceof C?i.im:0,m=u.data;return m[0]=s,m[1]=y,b.fromData(u.data,[],"complex128")}let t;if(a===void 0)if(r===void 0)t=Array.from({length:c},(u,i)=>i);else{t=[];for(let u=0;u<r.length;u++)t.push(c-r.length+u)}else t=a.map(u=>u<0?c+u:u);const n=[...l];if(r!==void 0)for(let u=0;u<r.length;u++){const i=t[u];n[i]=r[u]}let d=I(o);r!==void 0&&(d=B(d,n,t));for(const u of t)d=$(d,u,f,e);return d}function I(o){const r=o.dtype,a=Array.from(o.shape),e=o.size;if(r==="complex128"||r==="complex64"){const t=b.zeros(a,"complex128"),n=t.data,d=o.data;for(let u=0;u<e*2;u++)n[u]=d[u];return t}const f=b.zeros(a,"complex128"),l=f.data,c=o.data;if(r==="float64"){const t=c;for(let n=0;n<e;n++)l[n*2]=t[n]}else if(r==="float32"){const t=c;for(let n=0;n<e;n++)l[n*2]=t[n]}else if(r==="int32"){const t=c;for(let n=0;n<e;n++)l[n*2]=t[n]}else if(r==="int16"){const t=c;for(let n=0;n<e;n++)l[n*2]=t[n]}else if(r==="int8"){const t=c;for(let n=0;n<e;n++)l[n*2]=t[n]}else if(r==="uint32"){const t=c;for(let n=0;n<e;n++)l[n*2]=t[n]}else if(r==="uint16"){const t=c;for(let n=0;n<e;n++)l[n*2]=t[n]}else if(r==="uint8"){const t=c;for(let n=0;n<e;n++)l[n*2]=t[n]}else if(r==="int64"||r==="uint64"){const t=c;for(let n=0;n<e;n++)l[n*2]=Number(t[n])}else for(let t=0;t<e;t++)l[t*2]=Number(c[t]);return f}function B(o,r,a){const e=Array.from(o.shape);let f=o;for(const l of a){const c=e[l],t=r[l];c!==t&&(t>c?f=P(f,l,t):f=R(f,l,t),e[l]=t)}return f}function P(o,r,a){const e=Array.from(o.shape),f=e[r];e[r]=a;const l=b.zeros(e,"complex128"),c=l.data,t=o.data,n=e.slice(0,r).reduce((u,i)=>u*i,1),d=e.slice(r+1).reduce((u,i)=>u*i,1);for(let u=0;u<n;u++)for(let i=0;i<f;i++)for(let s=0;s<d;s++){const y=(u*f+i)*d+s,m=(u*a+i)*d+s;c[m*2]=t[y*2],c[m*2+1]=t[y*2+1]}return l}function R(o,r,a){const e=Array.from(o.shape),f=e[r];e[r]=a;const l=b.zeros(e,"complex128"),c=l.data,t=o.data,n=e.slice(0,r).reduce((u,i)=>u*i,1),d=e.slice(r+1).reduce((u,i)=>u*i,1);for(let u=0;u<n;u++)for(let i=0;i<a;i++)for(let s=0;s<d;s++){const y=(u*f+i)*d+s,m=(u*a+i)*d+s;c[m*2]=t[y*2],c[m*2+1]=t[y*2+1]}return l}function $(o,r,a,e){const f=Array.from(o.shape),l=f[r];if(l===0)return o;const c=b.zeros(f,"complex128"),t=c.data,n=o.data,d=f.slice(0,r).reduce((y,m)=>y*m,1),u=f.slice(r+1).reduce((y,m)=>y*m,1),i=new Float64Array(l),s=new Float64Array(l);for(let y=0;y<d;y++)for(let m=0;m<u;m++){for(let h=0;h<l;h++){const A=(y*l+h)*u+m;i[h]=n[A*2],s[h]=n[A*2+1]}if(U(i,s,a),e==="ortho"){const h=1/Math.sqrt(l);if(a){const A=Math.sqrt(l);for(let p=0;p<l;p++)i[p]=i[p]*A,s[p]=s[p]*A}else for(let A=0;A<l;A++)i[A]=i[A]*h,s[A]=s[A]*h}else if(e==="forward"&&!a){const h=1/l;for(let A=0;A<l;A++)i[A]=i[A]*h,s[A]=s[A]*h}else if(!(e==="backward"&&a)){if(e==="forward"&&a)for(let h=0;h<l;h++)i[h]=i[h]*l,s[h]=s[h]*l}for(let h=0;h<l;h++){const A=(y*l+h)*u+m;t[A*2]=i[h],t[A*2+1]=s[h]}}return c}function j(o,r,a=-1,e="backward"){const f=Array.from(o.shape),l=f.length,c=a<0?l+a:a,t=r??f[c],n=x(o,t,a,e),d=Math.floor(t/2)+1;return R(n,c,d)}function T(o,r,a=-1,e="backward"){const f=Array.from(o.shape),l=f.length,c=a<0?l+a:a,t=f[c],n=r??(t-1)*2,d=[...f];d[c]=n;const u=b.zeros(d,"complex128"),i=u.data,s=I(o).data,y=f.slice(0,c).reduce((A,p)=>A*p,1),m=f.slice(c+1).reduce((A,p)=>A*p,1);for(let A=0;A<y;A++)for(let p=0;p<m;p++){for(let g=0;g<t;g++){const S=(A*t+g)*m+p,w=(A*n+g)*m+p;i[w*2]=s[S*2],i[w*2+1]=s[S*2+1]}for(let g=t;g<n;g++){const S=n-g,w=(A*t+S)*m+p,F=(A*n+g)*m+p;i[F*2]=s[w*2],i[F*2+1]=-s[w*2+1]}}const h=M(u,n,a,e);return L(h)}function V(o,r,a=[-2,-1],e="backward"){const l=Array.from(o.shape).length,c=a[0]<0?l+a[0]:a[0],t=a[1]<0?l+a[1]:a[1];let n=x(o,r?r[0]:void 0,c,e);const d=Array.from(n.shape),u=r?r[1]:d[t];n=x(n,u,t,e);const i=Math.floor(u/2)+1;return R(n,t,i)}function W(o,r,a=[-2,-1],e="backward"){const f=Array.from(o.shape),l=f.length,c=a[0]<0?l+a[0]:a[0],t=a[1]<0?l+a[1]:a[1],n=f[t],d=r?r[1]:(n-1)*2,u=r?r[0]:f[c];let i=T(o,d,t,e);return i=M(i,u,c,e),L(i)}function X(o,r,a,e="backward"){const l=Array.from(o.shape).length;let c;if(a===void 0?c=Array.from({length:l},(u,i)=>i):c=a.map(u=>u<0?l+u:u),c.length===0)return I(o);let t=o;for(let u=0;u<c.length-1;u++){const i=c[u],s=r?r[u]:void 0;t=x(t,s,i,e)}const n=c[c.length-1],d=r?r[c.length-1]:void 0;return j(t,d,n,e)}function Y(o,r,a,e="backward"){const f=Array.from(o.shape),l=f.length;let c;if(a===void 0?c=Array.from({length:l},(i,s)=>s):c=a.map(i=>i<0?l+i:i),c.length===0)return L(I(o));const t=c[c.length-1],n=f[t],d=r?[...r]:c.map((i,s)=>s===c.length-1?(n-1)*2:f[i]);let u=T(o,d[c.length-1],t,e);for(let i=c.length-2;i>=0;i--){const s=c[i];u=M(u,d[i],s,e)}return L(u)}function Z(o,r,a=-1,e="backward"){const f=Array.from(o.shape),l=f.length,c=a<0?l+a:a,t=f[c],n=r??(t-1)*2,d=q(I(o)),u=T(d,n,a,e),i=u.data;for(let s=0;s<u.size;s++)i[s]=i[s]*n;return u}function tt(o,r,a=-1,e="backward"){const f=Array.from(o.shape),l=f.length,c=a<0?l+a:a,t=r??f[c],n=j(o,t,a,e),d=q(n),u=d.data;for(let i=0;i<d.size*2;i++)u[i]=u[i]/t;return d}function q(o){const r=Array.from(o.shape),a=o.size,e=b.zeros(r,"complex128"),f=e.data,l=o.data;for(let c=0;c<a;c++)f[c*2]=l[c*2],f[c*2+1]=-l[c*2+1];return e}function L(o){const r=Array.from(o.shape),a=o.size,e=b.zeros(r,"float64"),f=e.data,l=o.data;for(let c=0;c<a;c++)f[c]=l[c*2];return e}function rt(o,r=1){const a=b.zeros([o],"float64"),e=a.data,f=Math.floor((o-1)/2)+1;for(let l=0;l<f;l++)e[l]=l/(o*r);for(let l=f;l<o;l++)e[l]=(l-o)/(o*r);return a}function et(o,r=1){const a=Math.floor(o/2)+1,e=b.zeros([a],"float64"),f=e.data;for(let l=0;l<a;l++)f[l]=l/(o*r);return e}function ot(o,r){const a=Array.from(o.shape),e=a.length;let f;r===void 0?f=Array.from({length:e},(c,t)=>t):typeof r=="number"?f=[r<0?e+r:r]:f=r.map(c=>c<0?e+c:c);const l=a.map((c,t)=>f.includes(t)?Math.floor(a[t]/2):0);return N(o,l)}function nt(o,r){const a=Array.from(o.shape),e=a.length;let f;r===void 0?f=Array.from({length:e},(c,t)=>t):typeof r=="number"?f=[r<0?e+r:r]:f=r.map(c=>c<0?e+c:c);const l=a.map((c,t)=>f.includes(t)?-Math.floor(a[t]/2):0);return N(o,l)}function N(o,r){const a=Array.from(o.shape),e=o.dtype,f=o.size,l=b.zeros(a,e),c=e==="complex128"||e==="complex64",t=new Array(a.length);t[a.length-1]=1;for(let n=a.length-2;n>=0;n--)t[n]=t[n+1]*a[n+1];for(let n=0;n<f;n++){const d=new Array(a.length);let u=n;for(let y=0;y<a.length;y++)d[y]=Math.floor(u/t[y]),u=u%t[y];const i=d.map((y,m)=>{let h=y+r[m];const A=a[m];return h=(h%A+A)%A,h});let s=0;for(let y=0;y<a.length;y++)s+=i[y]*t[y];if(c){const y=o.data,m=l.data;m[s*2]=y[n*2],m[s*2+1]=y[n*2+1]}else{const y=o.iget(n);l.iset(s,y)}}return l}export{x as fft,G as fft2,rt as fftfreq,J as fftn,ot as fftshift,Z as hfft,M as ifft,H as ifft2,Q as ifftn,nt as ifftshift,tt as ihfft,T as irfft,W as irfft2,Y as irfftn,j as rfft,V as rfft2,et as rfftfreq,X as rfftn};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{Complex as z}from"../complex";const D={threshold:1e3,edgeitems:3,precision:8,linewidth:75,floatmode:"maxprec",suppress:!0,separator:" ",prefix:"",suffix:"",nanstr:"nan",infstr:"inf",sign:"-",legacy:!1};let x={...D};function q(n){x={...x,...n}}function E(){return{...x}}function R(n){let t=null;const e={_savedOptions:null,enter(){t={...x},e._savedOptions=t,x={...x,...n}},exit(){t&&(x=t,t=null,e._savedOptions=null)},apply(r){e.enter();try{return r()}finally{e.exit()}}};return e}function C(n,t=null,e=!0,r=!0,a="k",o="-",g=null,f=null,h=null){const u=t??x.precision;if(!Number.isFinite(n))return Number.isNaN(n)?x.nanstr:(n>0?"":"-")+x.infstr;let s;if(e&&t===null?(s=n.toString(),r&&!s.includes(".")&&!s.includes("e")&&(s+=".0")):s=n.toFixed(u),h!==null){const p=s.indexOf(".");if(p!==-1){const i=s.length-p-1;i<h&&(s+="0".repeat(h-i))}else r&&(s+="."+"0".repeat(h))}if(a!=="k"&&s.includes(".")){if(a==="."||a==="0"||a==="-"){let p=s.length;for(;p>0&&s[p-1]==="0";)p--;s=s.slice(0,p)}a==="0"&&s.endsWith(".")&&(s+="0"),a==="-"&&s.endsWith(".")&&(s=s.slice(0,-1))}if(n>=0&&!Object.is(n,-0)&&(o==="+"?s="+"+s:o===" "&&(s=" "+s)),g!==null&&s.length<g&&(s=" ".repeat(g-s.length)+s),f!==null){const p=s.indexOf(".");if(p!==-1){const i=s.length-p-1;i<f&&(s+="0".repeat(f-i))}}return s}function w(n,t=null,e=!0,r="k",a="-",o=null,g=2,f=null){const h=t??x.precision;if(!Number.isFinite(n))return Number.isNaN(n)?x.nanstr:(n>0?"":"-")+x.infstr;let u=n.toExponential(h);if(f!==null){const l=u.indexOf("e"),d=u.slice(0,l),S=u.slice(l),N=d.indexOf(".");if(N!==-1){const c=d.length-N-1;c<f&&(u=d+"0".repeat(f-c)+S)}}if(r!=="k"){const l=u.indexOf("e");let d=u.slice(0,l);const S=u.slice(l);d.includes(".")&&((r==="."||r==="0"||r==="-")&&(d=d.replace(/0+$/,"")),r==="0"&&d.endsWith(".")&&(d+="0"),r==="-"&&(d=d.replace(/\.$/,""))),u=d+S}const s=u.indexOf("e"),p=u.slice(0,s);let i=u.slice(s+1);const m=i[0]==="-"?"-":"+";let b=i.replace(/^[+-]/,"");for(;b.length<g;)b="0"+b;return u=p+"e"+m+b,n>=0&&!Object.is(n,-0)&&(a==="+"?u="+"+u:a===" "&&(u=" "+u)),o!==null&&u.length<o&&(u=" ".repeat(o-u.length)+u),u}function U(n,t=2,e=0){if(t<2||t>36)throw new Error("base must be between 2 and 36");n=Math.trunc(n);let r;if(n<0?r="-"+Math.abs(n).toString(t).toUpperCase():r=n.toString(t).toUpperCase(),e>0){const a="0".repeat(e);r.startsWith("-")?r="-"+a+r.slice(1):r=a+r}return r}function B(n,t=null){if(n=Math.trunc(n),t!==null&&n<0){if(n=Math.pow(2,t)+n,n<0)throw new Error("width too small for negative number");let a=n.toString(2);return a.length<t&&(a="0".repeat(t-a.length)+a),a}let e;return n<0?e="-"+Math.abs(n).toString(2):e=n.toString(2),t!==null&&e.length<t&&(e="0".repeat(t-e.length)+e),e}function P(n,t,e){if(n instanceof z){const r=P(n.re,"float64",e),a=P(Math.abs(n.im),"float64",e),o=n.im>=0?"+":"-";return`${r}${o}${a}j`}return typeof n=="boolean"?n?" True":"False":typeof n=="bigint"?n.toString():Number.isFinite(n)?t==="float32"||t==="float64"?e.suppress&&Math.abs(n)<1e-10&&n!==0?"0.":C(n,e.precision,!1,!0,"k",e.sign):n.toString():Number.isNaN(n)?e.nanstr:(n>0?"":"-")+e.infstr}function $(n,t){const e=[],r=n.shape,a=r.length,o=n.strides;function g(f,h){if(h===a){let i=0;for(let m=0;m<a;m++)i+=f[m]*o[m];e.push(n.iget(i));return}const u=r[h];if(r.reduce((i,m)=>i*m,1)>t.threshold&&u>2*t.edgeitems){for(let i=0;i<t.edgeitems;i++)f[h]=i,g(f,h+1);for(let i=u-t.edgeitems;i<u;i++)f[h]=i,g(f,h+1)}else for(let i=0;i<u;i++)f[h]=i,g(f,h+1)}return a>0?g(new Array(a).fill(0),0):e.push(n.iget(0)),e}function T(n,t){return n<0||Object.is(n,-0)?"-"+Math.abs(n).toString()+".":t==="+"?"+"+n.toString()+".":t===" "?" "+n.toString()+".":n.toString()+"."}function M(n,t,e){const r=n.indexOf(".");if(r===-1)return n.padStart(t+(e>0?1+e:0));const a=n.slice(0,r),o=n.slice(r+1),g=a.padStart(t),f=e>0?o.padEnd(e):o;return g+"."+f}function W(n){let t=0,e=0;for(const r of n){const a=r.indexOf(".");a===-1?t=Math.max(t,r.length):(t=Math.max(t,a),e=Math.max(e,r.length-a-1))}return{maxLeft:t,maxRight:e}}function A(n,t){const e=n.filter(i=>Number.isFinite(i));if(e.length===0){const i=n.map(b=>Number.isNaN(b)?t.nanstr:(b>0?"":"-")+t.infstr),m=i.length>0?Math.max(...i.map(b=>b.length)):1;return b=>Number.isNaN(b)?t.nanstr.padStart(m):Number.isFinite(b)?b.toString().padStart(m):((b>0?"":"-")+t.infstr).padStart(m)}const r=t.suppress?e.map(i=>Math.abs(i)<1e-10&&i!==0?0:i):e,a=r.map(Math.abs).filter(i=>i>0),o=a.length>0?Math.max(...a):0,g=a.length>0?Math.min(...a):0;if(o>=1e16||g>0&&g<1e-4||g>0&&o/g>1e3){const i=r.map(c=>w(c,t.precision,!1,".",t.sign));let m=0;for(const c of i){const y=c.indexOf("e"),O=y!==-1?c.slice(0,y):c,F=O.indexOf(".");F!==-1&&(m=Math.max(m,O.length-F-1))}const b=Math.max(m,0),l=Math.max(b,1),d=b===0?".":"k",S=r.map(c=>w(c,l,!1,d,t.sign));for(const c of n)Number.isFinite(c)||S.push(Number.isNaN(c)?t.nanstr:(c>0?"":"-")+t.infstr);const N=Math.max(...S.map(c=>c.length));return c=>(t.suppress&&Math.abs(c)<1e-10&&c!==0&&Number.isFinite(c)&&(c=0),Number.isNaN(c)?t.nanstr.padStart(N):Number.isFinite(c)?w(c,l,!1,d,t.sign).padStart(N):((c>0?"":"-")+t.infstr).padStart(N))}if(r.every(i=>Number.isInteger(i))){const i=r.map(l=>T(l,t.sign));for(const l of n)Number.isFinite(l)||i.push(Number.isNaN(l)?t.nanstr:(l>0?"":"-")+t.infstr);const{maxLeft:m,maxRight:b}=W(i);return l=>(t.suppress&&Math.abs(l)<1e-10&&l!==0&&Number.isFinite(l)&&(l=0),Number.isNaN(l)?M(t.nanstr,m,b):Number.isFinite(l)?M(T(l,t.sign),m,b):M((l>0?"":"-")+t.infstr,m,b))}const u=r.map(i=>C(i,t.precision,!1,!0,".",t.sign));for(const i of n)Number.isFinite(i)||u.push(Number.isNaN(i)?t.nanstr:(i>0?"":"-")+t.infstr);const{maxLeft:s,maxRight:p}=W(u);return i=>(t.suppress&&Math.abs(i)<1e-10&&i!==0&&Number.isFinite(i)&&(i=0),Number.isNaN(i)?M(t.nanstr,s,p):Number.isFinite(i)?M(C(i,t.precision,!1,!0,".",t.sign),s,p):M((i>0?"":"-")+t.infstr,s,p))}function j(n,t){const e=n.dtype,r=$(n,t);if(r.length===0)return o=>P(o,e,t);if(e==="bool")return o=>o?" True":"False";if(e==="int64"||e==="uint64"){const o=r.map(f=>f.toString()),g=Math.max(...o.map(f=>f.length));return f=>f.toString().padStart(g)}if(e.startsWith("int")||e.startsWith("uint")){const o=r.map(f=>f.toString()),g=Math.max(...o.map(f=>f.length));return f=>f.toString().padStart(g)}if(e==="complex64"||e==="complex128"){const o=r.map(u=>u.re),g=r.map(u=>Math.abs(u.im)),f=A(o,t),h=A(g,t);return u=>{const s=u,p=f(s.re),i=h(Math.abs(s.im)),m=s.im>=0?"+":"-";return`${p}${m}${i}j`}}const a=A(r,t);return o=>a(o)}function I(n,t,e,r,a,o){const g=n.shape,f=g.length;if(e===f){let l=0;const d=n.strides;for(let N=0;N<f;N++)l+=t[N]*d[N];const S=n.iget(l);return a(S)}const h=g[e],u=r.threshold,s=r.edgeitems,i=g.reduce((l,d)=>l*d,1)>u&&h>2*s,m=[],b=[...t];if(i){for(let l=0;l<s;l++)b[e]=l,m.push(I(n,b,e+1,r,a,o+1));m.push("...");for(let l=h-s;l<h;l++)b[e]=l,m.push(I(n,b,e+1,r,a,o+1))}else for(let l=0;l<h;l++)b[e]=l,m.push(I(n,b,e+1,r,a,o+1));if(e===f-1){const l="["+m.join(r.separator)+"]";if(o+l.length<r.linewidth)return l;const d=" ".repeat(o+1),S=r.linewidth-o-1,N=[];let c="";for(let y=0;y<m.length;y++){const O=m[y];if(c==="")c=O;else{const F=c+r.separator+O;F.length<S?c=F:(N.push(c),c=O)}}return c&&N.push(c),"["+N.join(`
|
|
2
|
+
`+d)+"]"}else{const l=" ".repeat(o+1),d=f-e-2,S=`
|
|
3
|
+
`+`
|
|
4
|
+
`.repeat(Math.max(0,d))+l;return"["+m.join(S)+"]"}}function k(n,t=null,e=null,r=null,a=" ",o="",g="",f=null,h=null,u=null,s=null){const p={...x,linewidth:t??x.linewidth,precision:e??x.precision,suppress:r??x.suppress,separator:a,prefix:o,suffix:g,threshold:f??x.threshold,edgeitems:h??x.edgeitems,floatmode:u??x.floatmode,sign:s??x.sign};if(n.ndim===0){const l=n.iget(0);return P(l,n.dtype,p)}const i=j(n,p),m=p.prefix.length,b=I(n,new Array(n.ndim).fill(0),0,p,i,m);return p.prefix+b+p.suffix}function G(n,t=null,e=null,r=null){let o="array("+k(n,t,e,r,", ");return n.dtype!=="float64"&&(o+=`, dtype='${n.dtype}'`),o+=")",o}function H(n,t=null,e=null,r=null){return k(n,t,e,r)}export{k as array2string,G as array_repr,H as array_str,U as base_repr,B as binary_repr,C as format_float_positional,w as format_float_scientific,E as get_printoptions,R as printoptions,q as set_printoptions};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as O}from"../storage";import{isBigIntDType as v,isComplexDType as H,promoteDTypes as et}from"../dtype";function mt(e,r=1,g=-1){if(r<0)throw new Error(`order must be non-negative but got ${r}`);if(r===0)return e.copy();const x=Array.from(e.shape),y=x.length,b=g<0?y+g:g;if(b<0||b>=y)throw new Error(`axis ${g} is out of bounds for array of dimension ${y}`);if(x[b]<r+1)throw new Error(`diff requires at least ${r+1} elements along axis ${g}, but got ${x[b]}`);let I=e;for(let w=0;w<r;w++)I=ot(I,b);return I}function ot(e,r){const g=Array.from(e.shape),x=g.length,y=g[r],b=[...g];b[r]=y-1;const I=e.dtype,w=H(I),N=v(I)?"float64":I,D=O.zeros(b,N),P=D.data,F=e.strides,R=D.size,p=e.offset;for(let d=0;d<R;d++){let n=d;const u=new Array(x);for(let t=x-1;t>=0;t--)u[t]=n%b[t],n=Math.floor(n/b[t]);const M=[...u],a=[...u];a[r]=M[r]+1;let o=0,s=0;for(let t=0;t<x;t++)o+=M[t]*F[t],s+=a[t]*F[t];if(w){const t=e.data,c=t[(p+o)*2],m=t[(p+o)*2+1],l=t[(p+s)*2],i=t[(p+s)*2+1];P[d*2]=l-c,P[d*2+1]=i-m}else{const t=(v(I),Number(e.data[p+o])),c=(v(I),Number(e.data[p+s]));P[d]=c-t}}return D}function lt(e,r=null,g=null){const x=e.size,y=e.dtype,b=H(y),I=v(y)?"float64":y,w=Math.max(0,x-1),N=g?g.length:0,D=r?r.length:0,P=N+w+D,F=O.zeros([P],I),R=F.data;let p=0;if(g)if(b)for(const d of g)R[p*2]=d,R[p*2+1]=0,p++;else for(const d of g)R[p++]=d;if(b)for(let d=0;d<w;d++){const n=e.iget(d),u=e.iget(d+1);R[p*2]=u.re-n.re,R[p*2+1]=u.im-n.im,p++}else for(let d=0;d<w;d++){const n=(v(y),Number(e.iget(d))),u=(v(y),Number(e.iget(d+1)));R[p++]=u-n}if(r)if(b)for(const d of r)R[p*2]=d,R[p*2+1]=0,p++;else for(const d of r)R[p++]=d;return F}function ut(e,r=1,g=null){const y=Array.from(e.shape).length;let b;if(g===null)b=Array.from({length:y},(N,D)=>D);else if(typeof g=="number"){const N=g<0?y+g:g;if(N<0||N>=y)throw new Error(`axis ${g} is out of bounds for array of dimension ${y}`);b=[N]}else b=g.map(N=>{const D=N<0?y+N:N;if(D<0||D>=y)throw new Error(`axis ${N} is out of bounds for array of dimension ${y}`);return D});let I;if(typeof r=="number")I=b.map(()=>r);else{if(r.length!==b.length)throw new Error("Number of spacings must match number of axes");I=r}const w=[];for(let N=0;N<b.length;N++)w.push(rt(e,b[N],I[N]));return w.length===1?w[0]:w}function rt(e,r,g){const x=Array.from(e.shape),y=x.length,b=x[r];if(b<2)throw new Error(`Shape of array along axis ${r} must be at least 2, but got ${b}`);const I=e.dtype,w=H(I),N=v(I)?"float64":I==="float32"?"float32":w?I:"float64",D=O.zeros(x,N),P=D.data,F=e.strides,R=g,p=2*R,d=e.size,n=e.offset;for(let u=0;u<d;u++){let M=u;const a=new Array(y);for(let t=y-1;t>=0;t--)a[t]=M%x[t],M=Math.floor(M/x[t]);let o=n;for(let t=0;t<y;t++)o+=a[t]*F[t];const s=a[r];if(w){const t=e.data,c=P;let m,l;if(s===0){const i=[...a];i[r]=1;let f=0;for(let S=0;S<y;S++)f+=i[S]*F[S];const h=t[o*2],A=t[o*2+1],z=t[(n+f)*2],$=t[(n+f)*2+1];m=(z-h)/R,l=($-A)/R}else if(s===b-1){const i=[...a];i[r]=b-2;let f=0;for(let S=0;S<y;S++)f+=i[S]*F[S];const h=t[o*2],A=t[o*2+1],z=t[(n+f)*2],$=t[(n+f)*2+1];m=(h-z)/R,l=(A-$)/R}else{const i=[...a],f=[...a];i[r]=s+1,f[r]=s-1;let h=0,A=0;for(let q=0;q<y;q++)h+=i[q]*F[q],A+=f[q]*F[q];const z=t[(n+h)*2],$=t[(n+h)*2+1],S=t[(n+A)*2],E=t[(n+A)*2+1];m=(z-S)/p,l=($-E)/p}c[u*2]=m,c[u*2+1]=l}else{let t;if(s===0){const c=[...a];c[r]=1;let m=0;for(let f=0;f<y;f++)m+=c[f]*F[f];const l=(v(I),Number(e.data[o]));t=((v(I),Number(e.data[n+m]))-l)/R}else if(s===b-1){const c=[...a];c[r]=b-2;let m=0;for(let f=0;f<y;f++)m+=c[f]*F[f];const l=(v(I),Number(e.data[o])),i=(v(I),Number(e.data[n+m]));t=(l-i)/R}else{const c=[...a],m=[...a];c[r]=s+1,m[r]=s-1;let l=0,i=0;for(let A=0;A<y;A++)l+=c[A]*F[A],i+=m[A]*F[A];const f=(v(I),Number(e.data[n+l])),h=(v(I),Number(e.data[n+i]));t=(f-h)/p}P[u]=t}}return D}function it(e,r,g=-1,x=-1,y=-1){const b=Array.from(e.shape),I=Array.from(r.shape),w=b.length,N=I.length,D=g<0?w+g:g,P=x<0?N+x:x;if(D<0||D>=w)throw new Error(`axisa ${g} is out of bounds for array of dimension ${w}`);if(P<0||P>=N)throw new Error(`axisb ${x} is out of bounds for array of dimension ${N}`);const F=b[D],R=I[P];if(F!==2&&F!==3)throw new Error(`incompatible dimensions for cross product (dimension must be 2 or 3, got ${F})`);if(R!==2&&R!==3)throw new Error(`incompatible dimensions for cross product (dimension must be 2 or 3, got ${R})`);const p=et(e.dtype,r.dtype),d=H(p),n=(a,o)=>{if(H(a.dtype)){const s=a.data,t=a.offset;return[s[(t+o)*2],s[(t+o)*2+1]]}return[Number(a.iget(o)),0]},u=(a,o,s,t)=>[a*s-o*t,a*t+o*s],M=(a,o,s,t)=>[a-s,o-t];if(w===1&&N===1&&F===3&&R===3){const a=O.zeros([3],p);if(d){const o=a.data,[s,t]=n(e,0),[c,m]=n(e,1),[l,i]=n(e,2),[f,h]=n(r,0),[A,z]=n(r,1),[$,S]=n(r,2),[E,q]=u(c,m,$,S),[C,B]=u(l,i,A,z),[T,J]=M(E,q,C,B),[V,j]=u(l,i,f,h),[k,G]=u(s,t,$,S),[K,L]=M(V,j,k,G),[Q,U]=u(s,t,A,z),[W,X]=u(c,m,f,h),[Y,Z]=M(Q,U,W,X);o[0]=T,o[1]=J,o[2]=K,o[3]=L,o[4]=Y,o[5]=Z}else{const o=a.data,s=Number(e.iget(0)),t=Number(e.iget(1)),c=Number(e.iget(2)),m=Number(r.iget(0)),l=Number(r.iget(1)),i=Number(r.iget(2));o[0]=t*i-c*l,o[1]=c*m-s*i,o[2]=s*l-t*m}return a}if(w===1&&N===1&&F===2&&R===2){const a=O.zeros([],p);if(d){const o=a.data,[s,t]=n(e,0),[c,m]=n(e,1),[l,i]=n(r,0),[f,h]=n(r,1),[A,z]=u(s,t,f,h),[$,S]=u(c,m,l,i),[E,q]=M(A,z,$,S);o[0]=E,o[1]=q}else{const o=Number(e.iget(0)),s=Number(e.iget(1)),t=Number(r.iget(0)),c=Number(r.iget(1));a.data[0]=o*c-s*t}return a}if(w===1&&N===1){if(F===2&&R===3){const a=O.zeros([3],p);if(d){const o=a.data,[s,t]=n(e,0),[c,m]=n(e,1),[l,i]=n(r,0),[f,h]=n(r,1),[A,z]=n(r,2),[$,S]=u(c,m,A,z),[E,q]=u(s,t,A,z),C=-E,B=-q,[T,J]=u(s,t,f,h),[V,j]=u(c,m,l,i),[k,G]=M(T,J,V,j);o[0]=$,o[1]=S,o[2]=C,o[3]=B,o[4]=k,o[5]=G}else{const o=a.data,s=Number(e.iget(0)),t=Number(e.iget(1)),c=Number(r.iget(0)),m=Number(r.iget(1)),l=Number(r.iget(2));o[0]=t*l,o[1]=-s*l,o[2]=s*m-t*c}return a}else if(F===3&&R===2){const a=O.zeros([3],p);if(d){const o=a.data,[s,t]=n(e,0),[c,m]=n(e,1),[l,i]=n(e,2),[f,h]=n(r,0),[A,z]=n(r,1),[$,S]=u(l,i,A,z),E=-$,q=-S,[C,B]=u(l,i,f,h),[T,J]=u(s,t,A,z),[V,j]=u(c,m,f,h),[k,G]=M(T,J,V,j);o[0]=E,o[1]=q,o[2]=C,o[3]=B,o[4]=k,o[5]=G}else{const o=a.data,s=Number(e.iget(0)),t=Number(e.iget(1)),c=Number(e.iget(2)),m=Number(r.iget(0)),l=Number(r.iget(1));o[0]=-c*l,o[1]=c*m,o[2]=s*l-t*m}return a}}if(w===2&&N===2&&D===1&&P===1){const a=b[0];if(I[0]!==a)throw new Error(`Shape mismatch: a has ${a} vectors, b has ${I[0]} vectors`);if(F===3&&R===3){const o=O.zeros([a,3],p);if(d){const s=o.data;for(let t=0;t<a;t++){const[c,m]=n(e,t*3),[l,i]=n(e,t*3+1),[f,h]=n(e,t*3+2),[A,z]=n(r,t*3),[$,S]=n(r,t*3+1),[E,q]=n(r,t*3+2),[C,B]=u(l,i,E,q),[T,J]=u(f,h,$,S),[V,j]=M(C,B,T,J),[k,G]=u(f,h,A,z),[K,L]=u(c,m,E,q),[Q,U]=M(k,G,K,L),[W,X]=u(c,m,$,S),[Y,Z]=u(l,i,A,z),[_,tt]=M(W,X,Y,Z);s[t*3*2]=V,s[t*3*2+1]=j,s[(t*3+1)*2]=Q,s[(t*3+1)*2+1]=U,s[(t*3+2)*2]=_,s[(t*3+2)*2+1]=tt}}else{const s=o.data;for(let t=0;t<a;t++){const c=Number(e.iget(t*3)),m=Number(e.iget(t*3+1)),l=Number(e.iget(t*3+2)),i=Number(r.iget(t*3)),f=Number(r.iget(t*3+1)),h=Number(r.iget(t*3+2));s[t*3]=m*h-l*f,s[t*3+1]=l*i-c*h,s[t*3+2]=c*f-m*i}}return o}if(F===2&&R===2){const o=O.zeros([a],p);if(d){const s=o.data;for(let t=0;t<a;t++){const[c,m]=n(e,t*2),[l,i]=n(e,t*2+1),[f,h]=n(r,t*2),[A,z]=n(r,t*2+1),[$,S]=u(c,m,A,z),[E,q]=u(l,i,f,h),[C,B]=M($,S,E,q);s[t*2]=C,s[t*2+1]=B}}else{const s=o.data;for(let t=0;t<a;t++){const c=Number(e.iget(t*2)),m=Number(e.iget(t*2+1)),l=Number(r.iget(t*2)),i=Number(r.iget(t*2+1));s[t]=c*i-m*l}}return o}}throw new Error(`cross product not implemented for arrays with shapes ${JSON.stringify(b)} and ${JSON.stringify(I)}`)}export{it as cross,mt as diff,lt as ediff1d,ut as gradient};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as z}from"../storage";import{elementwiseUnaryOp as g}from"../internal/compute";import{isComplexDType as F}from"../dtype";function x(a){const o=a.dtype;if(F(o)){const l=Array.from(a.shape),i=a.size,e=a.data,n=z.zeros(l,o),c=n.data;for(let t=0;t<i;t++){const s=e[t*2],r=e[t*2+1];c[t*2]=Math.sinh(s)*Math.cos(r),c[t*2+1]=Math.cosh(s)*Math.sin(r)}return n}return g(a,Math.sinh,!1)}function v(a){const o=a.dtype;if(F(o)){const l=Array.from(a.shape),i=a.size,e=a.data,n=z.zeros(l,o),c=n.data;for(let t=0;t<i;t++){const s=e[t*2],r=e[t*2+1];c[t*2]=Math.cosh(s)*Math.cos(r),c[t*2+1]=Math.sinh(s)*Math.sin(r)}return n}return g(a,Math.cosh,!1)}function b(a){const o=a.dtype;if(F(o)){const l=Array.from(a.shape),i=a.size,e=a.data,n=z.zeros(l,o),c=n.data;for(let t=0;t<i;t++){const s=e[t*2],r=e[t*2+1],p=Math.cosh(2*s)+Math.cos(2*r);c[t*2]=Math.sinh(2*s)/p,c[t*2+1]=Math.sin(2*r)/p}return n}return g(a,Math.tanh,!1)}function w(a){const o=a.dtype;if(F(o)){const l=Array.from(a.shape),i=a.size,e=a.data,n=z.zeros(l,o),c=n.data;for(let t=0;t<i;t++){const s=e[t*2],r=e[t*2+1],p=s*s-r*r,u=2*s*r,y=p+1,h=u,M=Math.sqrt(y*y+h*h),d=Math.sqrt((M+y)/2),f=(h>=0?1:-1)*Math.sqrt((M-y)/2),m=s+d,A=r+f,D=Math.sqrt(m*m+A*A);c[t*2]=Math.log(D),c[t*2+1]=Math.atan2(A,m)}return n}return g(a,Math.asinh,!1)}function C(a){const o=a.dtype;if(F(o)){const l=Array.from(a.shape),i=a.size,e=a.data,n=z.zeros(l,o),c=n.data;for(let t=0;t<i;t++){const s=e[t*2],r=e[t*2+1],p=s*s-r*r,u=2*s*r,y=p-1,h=u,M=Math.sqrt(y*y+h*h),d=Math.sqrt((M+y)/2),f=(h>=0?1:-1)*Math.sqrt((M-y)/2),m=s+d,A=r+f,D=Math.sqrt(m*m+A*A);let R=Math.log(D),I=Math.atan2(A,m);Math.abs(r)<1e-15&&s<1&&(I=-I),c[t*2]=R,c[t*2+1]=I}return n}return g(a,Math.acosh,!1)}function O(a){const o=a.dtype;if(F(o)){const l=Array.from(a.shape),i=a.size,e=a.data,n=z.zeros(l,o),c=n.data;for(let t=0;t<i;t++){const s=e[t*2],r=e[t*2+1],p=1+s,u=r,y=1-s,h=-r,M=y*y+h*h,d=(p*y+u*h)/M,f=(u*y-p*h)/M,m=Math.sqrt(d*d+f*f),A=Math.log(m),D=Math.atan2(f,d);c[t*2]=A/2,c[t*2+1]=D/2}return n}return g(a,Math.atanh,!1)}export{C as arccosh,w as arcsinh,O as arctanh,v as cosh,x as sinh,b as tanh};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{ArrayStorage as z}from"../storage";import{promoteDTypes as V,isComplexDType as k,isBigIntDType as J}from"../dtype";import{Complex as I}from"../complex";import*as R from"./shape";function T(t,e){if(t instanceof I||e instanceof I){const h=t instanceof I?t:new I(Number(t),0),p=e instanceof I?e:new I(Number(e),0);return h.mul(p)}return typeof t=="bigint"&&typeof e=="bigint"?Number(t*e):Number(t)*Number(e)}function ce(t,e,h,p,i,u,f,c,g){for(let o=0;o<h;o++)for(let s=0;s<u;s++)for(let l=0;l<f;l++){let n=0;for(let m=0;m<c;m++)n+=t[e+o*c+m]*p[i+s*c*f+m*f+l];g[o*u*f+s*f+l]=n}}function me(t,e,h,p,i,u,f,c,g,o){for(let s=0;s<h;s++)for(let l=0;l<f;l++){let n=0;for(let m=0;m<g;m++){const r=p===1?m:s*g+m,a=c===1?m:l*g+m;n+=t[e+r]*i[u+a]}o[h===1?l:s*f+l]=n}}function fe(t,e,h,p,i,u,f,c,g,o,s,l){for(let r=0;r<h*p;r++)s[r]=0;const n=t==="transpose",m=e==="transpose";if(!n&&!m)for(let r=0;r<h;r++)for(let a=0;a<p;a++){let b=0;for(let y=0;y<i;y++)b+=(f[r*c+y]??0)*(g[y*o+a]??0);s[r*l+a]=u*b}else if(n&&!m)for(let r=0;r<h;r++)for(let a=0;a<p;a++){let b=0;for(let y=0;y<i;y++)b+=(f[y*c+r]??0)*(g[y*o+a]??0);s[r*l+a]=u*b}else if(!n&&m)for(let r=0;r<h;r++)for(let a=0;a<p;a++){let b=0;for(let y=0;y<i;y++)b+=(f[r*c+y]??0)*(g[a*o+y]??0);s[r*l+a]=u*b}else for(let r=0;r<h;r++)for(let a=0;a<p;a++){let b=0;for(let y=0;y<i;y++)b+=(f[y*c+r]??0)*(g[a*o+y]??0);s[r*l+a]=u*b}}function X(t,e){const h=t.ndim,p=e.ndim,i=k(t.dtype)||k(e.dtype);if(h===0||p===0){const u=h===0?t.get():null,f=p===0?e.get():null;if(h===0&&p===0)return T(u,f);if(h===0){const c=V(t.dtype,e.dtype),g=z.zeros([...e.shape],c),o=(s,l)=>{const n=new Array(l.length);let m=s;for(let r=l.length-1;r>=0;r--)n[r]=m%l[r],m=Math.floor(m/l[r]);return n};for(let s=0;s<e.size;s++){const l=o(s,e.shape),n=e.get(...l);g.set(l,T(u,n))}return g}else{const c=V(t.dtype,e.dtype),g=z.zeros([...t.shape],c),o=(s,l)=>{const n=new Array(l.length);let m=s;for(let r=l.length-1;r>=0;r--)n[r]=m%l[r],m=Math.floor(m/l[r]);return n};for(let s=0;s<t.size;s++){const l=o(s,t.shape),n=t.get(...l);g.set(l,T(n,f))}return g}}if(h===1&&p===1){if(t.shape[0]!==e.shape[0])throw new Error(`dot: incompatible shapes (${t.shape[0]},) and (${e.shape[0]},)`);const u=t.shape[0];if(i){let c=0,g=0;for(let o=0;o<u;o++){const s=t.get(o),l=e.get(o),n=T(s,l);n instanceof I?(c+=n.re,g+=n.im):c+=n}return new I(c,g)}let f=0;for(let c=0;c<u;c++){const g=t.get(c),o=e.get(c);typeof g=="bigint"&&typeof o=="bigint"?f=Number(f)+Number(g*o):f+=Number(g)*Number(o)}return f}if(h===2&&p===2)return B(t,e);if(h===2&&p===1){const[u,f]=t.shape,c=e.shape[0];if(f!==c)throw new Error(`dot: incompatible shapes (${u},${f}) and (${c},)`);const g=V(t.dtype,e.dtype),o=z.zeros([u],g);if(i)for(let s=0;s<u;s++){let l=0,n=0;for(let m=0;m<f;m++){const r=t.get(s,m),a=e.get(m),b=r instanceof I?r:new I(Number(r),0),y=a instanceof I?a:new I(Number(a),0);l+=b.re*y.re-b.im*y.im,n+=b.re*y.im+b.im*y.re}o.set([s],new I(l,n))}else for(let s=0;s<u;s++){let l=0;for(let n=0;n<f;n++){const m=t.get(s,n),r=e.get(n);typeof m=="bigint"&&typeof r=="bigint"?l=Number(l)+Number(m*r):l+=Number(m)*Number(r)}o.set([s],l)}return o}if(h===1&&p===2){const u=t.shape[0],[f,c]=e.shape;if(u!==f)throw new Error(`dot: incompatible shapes (${u},) and (${f},${c})`);const g=V(t.dtype,e.dtype),o=z.zeros([c],g);if(i)for(let s=0;s<c;s++){let l=0,n=0;for(let m=0;m<u;m++){const r=t.get(m),a=e.get(m,s),b=r instanceof I?r:new I(Number(r),0),y=a instanceof I?a:new I(Number(a),0);l+=b.re*y.re-b.im*y.im,n+=b.re*y.im+b.im*y.re}o.set([s],new I(l,n))}else for(let s=0;s<c;s++){let l=0;for(let n=0;n<u;n++){const m=t.get(n),r=e.get(n,s);typeof m=="bigint"&&typeof r=="bigint"?l=Number(l)+Number(m*r):l+=Number(m)*Number(r)}o.set([s],l)}return o}if(h>2&&p===1){const u=t.shape[h-1],f=e.shape[0];if(u!==f)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and (${f},)`);const c=[...t.shape.slice(0,-1)],g=V(t.dtype,e.dtype),o=z.zeros(c,g),s=c.reduce((l,n)=>l*n,1);if(i)for(let l=0;l<s;l++){let n=0,m=0,r=l;const a=[];for(let b=c.length-1;b>=0;b--)a[b]=r%c[b],r=Math.floor(r/c[b]);for(let b=0;b<u;b++){const y=[...a,b],w=t.get(...y),d=e.get(b),x=w instanceof I?w:new I(Number(w),0),A=d instanceof I?d:new I(Number(d),0);n+=x.re*A.re-x.im*A.im,m+=x.re*A.im+x.im*A.re}o.set(a,new I(n,m))}else for(let l=0;l<s;l++){let n=0,m=l;const r=[];for(let a=c.length-1;a>=0;a--)r[a]=m%c[a],m=Math.floor(m/c[a]);for(let a=0;a<u;a++){const b=[...r,a],y=t.get(...b),w=e.get(a);typeof y=="bigint"&&typeof w=="bigint"?n=Number(n)+Number(y*w):n+=Number(y)*Number(w)}o.set(r,n)}return o}if(h===1&&p>2){const u=t.shape[0],f=1,c=e.shape[f];if(u!==c)throw new Error(`dot: incompatible shapes (${u},) and ${JSON.stringify(e.shape)}`);const g=[...e.shape.slice(0,f),...e.shape.slice(f+1)],o=V(t.dtype,e.dtype),s=z.zeros(g,o),l=g.reduce((n,m)=>n*m,1);if(i)for(let n=0;n<l;n++){let m=n;const r=[];for(let d=g.length-1;d>=0;d--)r[d]=m%g[d],m=Math.floor(m/g[d]);const a=r.slice(0,f),b=r.slice(f);let y=0,w=0;for(let d=0;d<u;d++){const x=t.get(d),A=[...a,d,...b],S=e.get(...A),v=x instanceof I?x:new I(Number(x),0),N=S instanceof I?S:new I(Number(S),0);y+=v.re*N.re-v.im*N.im,w+=v.re*N.im+v.im*N.re}s.set(r,new I(y,w))}else for(let n=0;n<l;n++){let m=n;const r=[];for(let w=g.length-1;w>=0;w--)r[w]=m%g[w],m=Math.floor(m/g[w]);const a=r.slice(0,f),b=r.slice(f);let y=0;for(let w=0;w<u;w++){const d=t.get(w),x=[...a,w,...b],A=e.get(...x);typeof d=="bigint"&&typeof A=="bigint"?y=Number(y)+Number(d*A):y+=Number(d)*Number(A)}s.set(r,y)}return s}if(h>=2&&p>=2&&!(h===2&&p===2)){const u=t.shape[h-1],f=e.shape[p-2];if(u!==f)throw new Error(`dot: incompatible shapes ${JSON.stringify(t.shape)} and ${JSON.stringify(e.shape)}`);const c=[...t.shape.slice(0,-1),...e.shape.slice(0,-2),e.shape[p-1]],g=V(t.dtype,e.dtype),o=z.zeros(c,g),s=t.shape.slice(0,-1).reduce((r,a)=>r*a,1),l=e.shape.slice(0,-2).reduce((r,a)=>r*a,1),n=e.shape[p-1],m=u;if(i)for(let r=0;r<s;r++)for(let a=0;a<l;a++)for(let b=0;b<n;b++){let y=0,w=0;for(let A=0;A<m;A++){const S=[];let v=r;for(let j=t.shape.length-2;j>=0;j--)S.unshift(v%t.shape[j]),v=Math.floor(v/t.shape[j]);S.push(A);const N=t.get(...S),M=[];let $=a;for(let j=e.shape.length-3;j>=0;j--)M.unshift($%e.shape[j]),$=Math.floor($/e.shape[j]);M.push(A,b);const D=e.get(...M),C=N instanceof I?N:new I(Number(N),0),E=D instanceof I?D:new I(Number(D),0);y+=C.re*E.re-C.im*E.im,w+=C.re*E.im+C.im*E.re}const d=r*l*n+a*n+b,x=o.data;x[d*2]=y,x[d*2+1]=w}else if(t.isCContiguous&&e.isCContiguous&&!J(t.dtype)&&!J(e.dtype))ce(t.data,t.offset,s,e.data,e.offset,l,n,m,o.data);else for(let r=0;r<s;r++)for(let a=0;a<l;a++)for(let b=0;b<n;b++){let y=0;for(let d=0;d<m;d++){const x=r*m+d,A=a*m*n+d*n+b,S=t.iget(x),v=e.iget(A);typeof S=="bigint"&&typeof v=="bigint"?y=Number(y)+Number(S*v):y+=Number(S)*Number(v)}const w=r*l*n+a*n+b;o.data[w]=y}return o}throw new Error(`dot: unexpected combination of dimensions ${h}D \xB7 ${p}D`)}function B(t,e){if(t.ndim!==2||e.ndim!==2)throw new Error("matmul requires 2D arrays");const[h=0,p=0]=t.shape,[i=0,u=0]=e.shape;if(p!==i)throw new Error(`matmul shape mismatch: (${h},${p}) @ (${i},${u})`);const f=V(t.dtype,e.dtype);if(k(f)){const A=z.zeros([h,u],f),S=A.data;for(let v=0;v<h;v++)for(let N=0;N<u;N++){let M=0,$=0;for(let C=0;C<p;C++){const E=t.iget(v*p+C),j=e.iget(C*u+N);M+=E.re*j.re-E.im*j.im,$+=E.re*j.im+E.im*j.re}const D=v*u+N;S[D*2]=M,S[D*2+1]=$}return A}const c=f.startsWith("int")||f.startsWith("uint")||f==="bool"?"float64":f;if(c!=="float64")throw new Error(`matmul currently only supports float64, got ${c}`);let g=t.dtype==="float64"?t.data:Float64Array.from(Array.from(t.data).map(Number)),o=e.dtype==="float64"?e.data:Float64Array.from(Array.from(e.data).map(Number));t.offset>0&&(g=g.subarray(t.offset)),e.offset>0&&(o=o.subarray(e.offset));const[s=0,l=0]=t.strides,[n=0,m=0]=e.strides,r=l>s,a=m>n,b=r?"transpose":"no-transpose",y=a?"transpose":"no-transpose";let w,d;r?w=l:w=s,a?d=m:d=n;const x=z.zeros([h,u],"float64");return fe(b,y,h,u,p,1,g,w,o,d,x.data,u),x}function $e(t){if(t.ndim!==2)throw new Error(`trace requires 2D array, got ${t.ndim}D`);const[e=0,h=0]=t.shape,p=Math.min(e,h);if(k(t.dtype)){let u=0,f=0;for(let c=0;c<p;c++){const g=t.get(c,c);u+=g.re,f+=g.im}return new I(u,f)}let i=0;for(let u=0;u<p;u++){const f=t.get(u,u);typeof f=="bigint"?i=(typeof i=="bigint"?i:BigInt(i))+f:i=(typeof i=="bigint"?Number(i):i)+f}return i}function L(t,e){return R.transpose(t,e)}function Me(t,e){const h=t.ndim,p=e.ndim,i=k(t.dtype)||k(e.dtype),u=t.shape[h-1],f=e.shape[p-1];if(u!==f)throw new Error(`inner: incompatible shapes - last dimensions ${u} and ${f} don't match`);if(h===1&&p===1)return X(t,e);const c=[...t.shape.slice(0,-1),...e.shape.slice(0,-1)],g=V(t.dtype,e.dtype),o=z.zeros(c,g),s=h===1?1:t.shape.slice(0,-1).reduce((m,r)=>m*r,1),l=p===1?1:e.shape.slice(0,-1).reduce((m,r)=>m*r,1),n=u;if(i)for(let m=0;m<s;m++)for(let r=0;r<l;r++){let a=0,b=0;for(let d=0;d<n;d++){let x,A;if(h===1)x=t.get(d);else{const N=[];let M=m;const $=t.shape.slice(0,-1);for(let D=$.length-1;D>=0;D--)N.unshift(M%$[D]),M=Math.floor(M/$[D]);N.push(d),x=t.get(...N)}if(p===1)A=e.get(d);else{const N=[];let M=r;const $=e.shape.slice(0,-1);for(let D=$.length-1;D>=0;D--)N.unshift(M%$[D]),M=Math.floor(M/$[D]);N.push(d),A=e.get(...N)}const S=x instanceof I?x:new I(Number(x),0),v=A instanceof I?A:new I(Number(A),0);a+=S.re*v.re-S.im*v.im,b+=S.re*v.im+S.im*v.re}if(c.length===0)return new I(a,b);const y=s===1?r:m*l+r,w=o.data;w[y*2]=a,w[y*2+1]=b}else if(t.isCContiguous&&e.isCContiguous&&!J(t.dtype)&&!J(e.dtype)){if(c.length===0){const m=t.data,r=e.data,a=t.offset,b=e.offset;let y=0;for(let w=0;w<n;w++)y+=m[a+w]*r[b+w];return y}me(t.data,t.offset,s,h,e.data,e.offset,l,p,n,o.data)}else for(let m=0;m<s;m++)for(let r=0;r<l;r++){let a=0;for(let y=0;y<n;y++){const w=h===1?y:m*n+y,d=p===1?y:r*n+y,x=t.iget(w),A=e.iget(d);typeof x=="bigint"&&typeof A=="bigint"?a=Number(a)+Number(x*A):a+=Number(x)*Number(A)}if(c.length===0)return a;const b=s===1?r:m*l+r;o.data[b]=a}return o}function pe(t,e){const h=t.ndim===1?t:R.ravel(t),p=e.ndim===1?e:R.ravel(e),i=h.size,u=p.size,f=V(t.dtype,e.dtype),c=z.zeros([i,u],f);for(let g=0;g<i;g++)for(let o=0;o<u;o++){const s=h.get(g),l=p.get(o),n=T(s,l);c.set([g,o],n)}return c}function De(t,e,h){let p,i;if(typeof h=="number"){const r=h;if(r<0)throw new Error("tensordot: axes must be non-negative");if(r>t.ndim||r>e.ndim)throw new Error("tensordot: axes exceeds array dimensions");p=Array.from({length:r},(a,b)=>t.ndim-r+b),i=Array.from({length:r},(a,b)=>b)}else if([p,i]=h,p.length!==i.length)throw new Error("tensordot: axes lists must have same length");for(let r=0;r<p.length;r++){const a=p[r],b=i[r];if(a<0||a>=t.ndim||b<0||b>=e.ndim)throw new Error("tensordot: axis out of bounds");if(t.shape[a]!==e.shape[b])throw new Error(`tensordot: shape mismatch on axes ${a} and ${b}: ${t.shape[a]} != ${e.shape[b]}`)}const u=[],f=[];for(let r=0;r<t.ndim;r++)p.includes(r)||u.push(r);for(let r=0;r<e.ndim;r++)i.includes(r)||f.push(r);const c=[...u.map(r=>t.shape[r]),...f.map(r=>e.shape[r])],g=V(t.dtype,e.dtype),o=k(g),s=r=>r instanceof I?{re:r.re,im:r.im}:{re:Number(r),im:0};if(c.length===0){let r=0,a=0;const b=p.map(y=>t.shape[y]).reduce((y,w)=>y*w,1);for(let y=0;y<b;y++){let w=y;const d=new Array(p.length);for(let N=p.length-1;N>=0;N--){const M=p[N];d[N]=w%t.shape[M],w=Math.floor(w/t.shape[M])}const x=new Array(t.ndim),A=new Array(e.ndim);for(let N=0;N<p.length;N++)x[p[N]]=d[N];for(let N=0;N<i.length;N++)A[i[N]]=d[N];const S=t.get(...x),v=e.get(...A);if(o){const N=s(S),M=s(v);r+=N.re*M.re-N.im*M.im,a+=N.re*M.im+N.im*M.re}else typeof S=="bigint"&&typeof v=="bigint"?r+=Number(S*v):r+=Number(S)*Number(v)}return o?new I(r,a):r}const l=z.zeros(c,g),n=c.reduce((r,a)=>r*a,1),m=p.map(r=>t.shape[r]).reduce((r,a)=>r*a,1);for(let r=0;r<n;r++){let a=r;const b=[];for(let A=c.length-1;A>=0;A--)b[A]=a%c[A],a=Math.floor(a/c[A]);const y=b.slice(0,u.length),w=b.slice(u.length);let d=0,x=0;for(let A=0;A<m;A++){a=A;const S=[];for(let D=p.length-1;D>=0;D--){const C=p[D];S[D]=a%t.shape[C],a=Math.floor(a/t.shape[C])}const v=new Array(t.ndim),N=new Array(e.ndim);for(let D=0;D<u.length;D++)v[u[D]]=y[D];for(let D=0;D<f.length;D++)N[f[D]]=w[D];for(let D=0;D<p.length;D++)v[p[D]]=S[D],N[i[D]]=S[D];const M=t.get(...v),$=e.get(...N);if(o){const D=s(M),C=s($);d+=D.re*C.re-D.im*C.im,x+=D.re*C.im+D.im*C.re}else typeof M=="bigint"&&typeof $=="bigint"?d+=Number(M*$):d+=Number(M)*Number($)}o?l.set(b,new I(d,x)):l.set(b,d)}return l}function ze(t,e=0,h=0,p=1){const i=t.shape,u=i.length;if(u<2)throw new Error("diagonal requires an array of at least two dimensions");const f=h<0?u+h:h,c=p<0?u+p:p;if(f<0||f>=u||c<0||c>=u)throw new Error("axis out of bounds");if(f===c)throw new Error("axis1 and axis2 cannot be the same");const g=i[f],o=i[c];let s;e>=0?s=Math.max(0,Math.min(g,o-e)):s=Math.max(0,Math.min(g+e,o));const l=[];for(let a=0;a<u;a++)a!==f&&a!==c&&l.push(i[a]);l.push(s);const n=z.zeros(l,t.dtype),m=i.filter((a,b)=>b!==f&&b!==c),r=m.reduce((a,b)=>a*b,1);for(let a=0;a<r;a++){let b=a;const y=[];for(let w=m.length-1;w>=0;w--)y.unshift(b%m[w]),b=Math.floor(b/m[w]);for(let w=0;w<s;w++){const d=new Array(u);let x=0;for(let v=0;v<u;v++)v===f?d[v]=e>=0?w:w-e:v===c?d[v]=e>=0?w+e:w:d[v]=y[x++];const A=[...y,w],S=t.get(...d);n.set(A,S)}}return n}function Ee(t,...e){const h=t.indexOf("->");let p,i;h===-1?(p=t,i=ne(p)):(p=t.slice(0,h),i=t.slice(h+2));const u=p.split(",").map(a=>a.trim());if(u.length!==e.length)throw new Error(`einsum: expected ${u.length} operands, got ${e.length}`);const f=new Map;for(let a=0;a<e.length;a++){const b=u[a],y=e[a];if(b.length!==y.ndim)throw new Error(`einsum: operand ${a} has ${y.ndim} dimensions but subscript '${b}' has ${b.length} indices`);for(let w=0;w<b.length;w++){const d=b[w],x=y.shape[w];if(f.has(d)){if(f.get(d)!==x)throw new Error(`einsum: size mismatch for index '${d}': ${f.get(d)} vs ${x}`)}else f.set(d,x)}}for(const a of i)if(!f.has(a))throw new Error(`einsum: output subscript contains unknown index '${a}'`);const c=new Set(i),g=new Set;for(const a of u)for(const b of a)g.add(b);const o=[];for(const a of g)c.has(a)||o.push(a);if(e.length===2&&u.length===2){const[a,b]=u,[y,w]=e;if(a.length===2&&b.length===2&&i.length===2&&y.ndim===2&&w.ndim===2){const[d,x]=[a[0],a[1]],[A,S]=[b[0],b[1]],[v,N]=[i[0],i[1]];if(d===v&&S===N&&x===A&&o.length===1&&o[0]===x||d===v&&S===N&&x===A&&o.length===1&&o[0]===x)return B(y,w);if(x===v&&S===N&&d===A&&o.length===1&&o[0]===d){const M=L(y);return B(M,w)}if(d===v&&A===N&&x===S&&o.length===1&&o[0]===x){const M=L(w);return B(y,M)}}if(a.length===1&&b.length===1&&a===b&&i.length===0&&y.ndim===1&&w.ndim===1)return Y(e,u,o,f);if(a&&b&&a.length===1&&b.length===1&&i.length===2&&i===a+b&&o.length===0&&y.ndim===1&&w.ndim===1)return pe(y,w)}if(e.length===1&&u[0].length===2&&i.length===0){const a=u[0];if(a[0]===a[1]&&e[0].ndim===2)return Y(e,u,o,f)}const s=Array.from(i).map(a=>f.get(a));if(s.length===0)return Y(e,u,o,f);let l=e[0].dtype;for(let a=1;a<e.length;a++)l=V(l,e[a].dtype);const n=z.zeros(s,l),m=s.reduce((a,b)=>a*b,1);let r=1;for(const a of o)r*=f.get(a);for(let a=0;a<m;a++){const b=he(a,s),y=new Map;for(let d=0;d<i.length;d++)y.set(i[d],b[d]);let w=0;for(let d=0;d<r;d++){let x=d;for(let S=o.length-1;S>=0;S--){const v=o[S],N=f.get(v);y.set(v,x%N),x=Math.floor(x/N)}let A=1;for(let S=0;S<e.length;S++){const v=e[S],N=u[S],M=[];for(const D of N)M.push(y.get(D));const $=v.get(...M);A*=Number($)}w+=A}n.set(b,w)}return n}function ne(t){const e=new Map,h=t.split(",");for(const i of h)for(const u of i.trim())e.set(u,(e.get(u)||0)+1);const p=[];for(const[i,u]of e)u===1&&p.push(i);return p.sort().join("")}function Y(t,e,h,p){let i=!1;for(const o of t)if(k(o.dtype)){i=!0;break}const u=o=>o instanceof I?{re:o.re,im:o.im}:{re:Number(o),im:0};let f=1;for(const o of h)f*=p.get(o);let c=0,g=0;for(let o=0;o<f;o++){const s=new Map;let l=o;for(let r=h.length-1;r>=0;r--){const a=h[r],b=p.get(a);s.set(a,l%b),l=Math.floor(l/b)}let n=1,m=0;for(let r=0;r<t.length;r++){const a=t[r],b=e[r],y=[];for(const d of b)y.push(s.get(d));const w=a.get(...y);if(i){const d=u(w),x=n*d.re-m*d.im,A=n*d.im+m*d.re;n=x,m=A}else n*=Number(w)}c+=n,g+=m}return i?new I(c,g):c}function he(t,e){const h=new Array(e.length);let p=t;for(let i=e.length-1;i>=0;i--)h[i]=p%e[i],p=Math.floor(p/e[i]);return h}function Ce(t,e){const h=t.shape,p=e.shape,i=h.length,u=p.length,f=V(t.dtype,e.dtype),c=Math.max(i,u),g=new Array(c),o=new Array(c).fill(1),s=new Array(c).fill(1);for(let r=0;r<i;r++)o[c-i+r]=h[r];for(let r=0;r<u;r++)s[c-u+r]=p[r];for(let r=0;r<c;r++)g[r]=o[r]*s[r];const l=z.zeros(g,f),n=h.reduce((r,a)=>r*a,1),m=p.reduce((r,a)=>r*a,1);for(let r=0;r<n;r++){let a=r;const b=new Array(i);for(let d=i-1;d>=0;d--)b[d]=a%h[d],a=Math.floor(a/h[d]);const y=new Array(c).fill(0);for(let d=0;d<i;d++)y[c-i+d]=b[d];const w=t.get(...b);for(let d=0;d<m;d++){let x=d;const A=new Array(u);for(let $=u-1;$>=0;$--)A[$]=x%p[$],x=Math.floor(x/p[$]);const S=new Array(c).fill(0);for(let $=0;$<u;$++)S[c-u+$]=A[$];const v=e.get(...A),N=new Array(c);for(let $=0;$<c;$++)N[$]=y[$]*s[$]+S[$];const M=T(w,v);l.set(N,M)}}return l}function je(t,e,h=-1,p=-1,i=-1,u){u!==void 0&&(h=u,p=u,i=u);const f=(N,M)=>N<0?M+N:N,c=f(h,t.ndim),g=f(p,e.ndim),o=V(t.dtype,e.dtype),s=k(o),l=(N,...M)=>{const $=N.get(...M);return $ instanceof I?$:Number($)},n=(N,M)=>{if(N instanceof I||M instanceof I){const $=N instanceof I?N:new I(N,0),D=M instanceof I?M:new I(M,0);return $.mul(D)}return N*M},m=(N,M)=>{if(N instanceof I||M instanceof I){const $=N instanceof I?N:new I(N,0),D=M instanceof I?M:new I(M,0);return $.sub(D)}return N-M};if(t.ndim===1&&e.ndim===1){const N=t.shape[0],M=e.shape[0];if(N===3&&M===3){const $=l(t,0),D=l(t,1),C=l(t,2),E=l(e,0),j=l(e,1),F=l(e,2),q=z.zeros([3],o);return q.set([0],m(n(D,F),n(C,j))),q.set([1],m(n(C,E),n($,F))),q.set([2],m(n($,j),n(D,E))),q}else if(N===2&&M===2){const $=l(t,0),D=l(t,1),C=l(e,0),E=l(e,1);return m(n($,E),n(D,C))}else if(N===2&&M===3||N===3&&M===2){const $=l(t,0),D=l(t,1),C=N===3?l(t,2):s?new I(0,0):0,E=l(e,0),j=l(e,1),F=M===3?l(e,2):s?new I(0,0):0,q=z.zeros([3],o);return q.set([0],m(n(D,F),n(C,j))),q.set([1],m(n(C,E),n($,F))),q.set([2],m(n($,j),n(D,E))),q}else throw new Error(`cross: incompatible dimensions for cross product: ${N} and ${M}`)}const r=t.shape[c],a=e.shape[g];if(r!==2&&r!==3||a!==2&&a!==3)throw new Error(`cross: incompatible dimensions for cross product: ${r} and ${a}`);const b=r===2&&a===2?0:3,y=[...t.shape.slice(0,c),...t.shape.slice(c+1)],w=[...e.shape.slice(0,g),...e.shape.slice(g+1)];if(y.length!==w.length)throw new Error("cross: incompatible shapes for cross product");for(let N=0;N<y.length;N++)if(y[N]!==w[N])throw new Error("cross: incompatible shapes for cross product");const d=y,x=i<0?d.length+1+i:i;let A;if(b===0?A=d:A=[...d.slice(0,x),b,...d.slice(x)],A.length===0)throw new Error("cross: unexpected scalar result from higher-dimensional input");const S=z.zeros(A,o),v=d.reduce((N,M)=>N*M,1);for(let N=0;N<v;N++){let M=N;const $=[];for(let _=d.length-1;_>=0;_--)$[_]=M%d[_],M=Math.floor(M/d[_]);const D=[...$.slice(0,c),0,...$.slice(c)],C=[...$.slice(0,g),0,...$.slice(g)],E=_=>(D[c]=_,l(t,...D)),j=_=>(C[g]=_,l(e,...C)),F=E(0),q=E(1),te=r===3?E(2):s?new I(0,0):0,U=j(0),G=j(1),re=a===3?j(2):s?new I(0,0):0;if(b===0)S.set($,m(n(F,G),n(q,U)));else{const _=m(n(q,re),n(te,G)),se=m(n(te,U),n(F,re)),ie=m(n(F,G),n(q,U)),H=(le,ae)=>{const ue=[...$.slice(0,x),le,...$.slice(x)];S.set(ue,ae)};H(0,_),H(1,se),H(2,ie)}}return S}function W(t,e=2,h,p=!1){if(typeof e!="number")throw new Error("vector_norm: ord must be a number");if(h==null){const o=t.ndim===1?t:R.ravel(t),s=o.size;let l;if(e===1/0){l=0;for(let n=0;n<s;n++)l=Math.max(l,Math.abs(Number(o.get(n))))}else if(e===-1/0){l=1/0;for(let n=0;n<s;n++)l=Math.min(l,Math.abs(Number(o.get(n))))}else if(e===0){l=0;for(let n=0;n<s;n++)Number(o.get(n))!==0&&l++}else if(e===1){l=0;for(let n=0;n<s;n++)l+=Math.abs(Number(o.get(n)))}else if(e===2){l=0;for(let n=0;n<s;n++){const m=Number(o.get(n));l+=m*m}l=Math.sqrt(l)}else{l=0;for(let n=0;n<s;n++)l+=Math.pow(Math.abs(Number(o.get(n))),e);l=Math.pow(l,1/e)}if(p){const n=new Array(t.ndim).fill(1),m=z.zeros(n,"float64");return m.set(new Array(t.ndim).fill(0),l),m}return l}const i=h<0?t.ndim+h:h;if(i<0||i>=t.ndim)throw new Error(`vector_norm: axis ${h} out of bounds for array with ${t.ndim} dimensions`);const u=p?[...t.shape.slice(0,i),1,...t.shape.slice(i+1)]:[...t.shape.slice(0,i),...t.shape.slice(i+1)];if(u.length===0)return W(t,e,null,!1);const f=z.zeros(u,"float64"),c=t.shape[i],g=u.reduce((o,s)=>o*s,1);for(let o=0;o<g;o++){let s=o;const l=[];for(let r=u.length-1;r>=0;r--)l[r]=s%u[r],s=Math.floor(s/u[r]);const n=p?[...l.slice(0,i),0,...l.slice(i+1)]:[...l.slice(0,i),0,...l.slice(i)];let m;if(e===1/0){m=0;for(let r=0;r<c;r++)n[i]=r,m=Math.max(m,Math.abs(Number(t.get(...n))))}else if(e===-1/0){m=1/0;for(let r=0;r<c;r++)n[i]=r,m=Math.min(m,Math.abs(Number(t.get(...n))))}else if(e===0){m=0;for(let r=0;r<c;r++)n[i]=r,Number(t.get(...n))!==0&&m++}else if(e===1){m=0;for(let r=0;r<c;r++)n[i]=r,m+=Math.abs(Number(t.get(...n)))}else if(e===2){m=0;for(let r=0;r<c;r++){n[i]=r;const a=Number(t.get(...n));m+=a*a}m=Math.sqrt(m)}else{m=0;for(let r=0;r<c;r++)n[i]=r,m+=Math.pow(Math.abs(Number(t.get(...n))),e);m=Math.pow(m,1/e)}f.set(l,m)}return f}function P(t,e="fro",h=!1){if(t.ndim!==2)throw new Error(`matrix_norm: input must be 2D, got ${t.ndim}D`);const[p,i]=t.shape;let u;if(e==="fro"){u=0;for(let f=0;f<p;f++)for(let c=0;c<i;c++){const g=Number(t.get(f,c));u+=g*g}u=Math.sqrt(u)}else if(e==="nuc"){const{s:f}=O(t);u=0;for(let c=0;c<f.size;c++)u+=Number(f.get(c))}else if(e===1){u=0;for(let f=0;f<i;f++){let c=0;for(let g=0;g<p;g++)c+=Math.abs(Number(t.get(g,f)));u=Math.max(u,c)}}else if(e===-1){u=1/0;for(let f=0;f<i;f++){let c=0;for(let g=0;g<p;g++)c+=Math.abs(Number(t.get(g,f)));u=Math.min(u,c)}}else if(e===1/0){u=0;for(let f=0;f<p;f++){let c=0;for(let g=0;g<i;g++)c+=Math.abs(Number(t.get(f,g)));u=Math.max(u,c)}}else if(e===-1/0){u=1/0;for(let f=0;f<p;f++){let c=0;for(let g=0;g<i;g++)c+=Math.abs(Number(t.get(f,g)));u=Math.min(u,c)}}else if(e===2){const{s:f}=O(t);u=Number(f.get(0))}else if(e===-2){const{s:f}=O(t);u=Number(f.get(f.size-1))}else throw new Error(`matrix_norm: invalid ord value: ${e}`);if(h){const f=z.zeros([1,1],"float64");return f.set([0,0],u),f}return u}function Ve(t,e=null,h=null,p=!1){if(e===null)return h===null?W(t,2,null,p):typeof h=="number"?W(t,2,h,p):P(t,"fro",p);if(Array.isArray(h)){if(h.length!==2)throw new Error("norm: axis must be a 2-tuple for matrix norms");const i=h[0]<0?t.ndim+h[0]:h[0],u=h[1]<0?t.ndim+h[1]:h[1];if(t.ndim!==2||i!==0&&i!==1||u!==0&&u!==1||i===u)throw new Error("norm: complex axis specification not yet supported");return P(t,e,p)}if(t.ndim===2&&h===null&&(e==="fro"||e==="nuc"))return P(t,e,p);if(typeof e!="number"&&e!==null)throw new Error(`norm: ord '${e}' not valid for vector norm`);return W(t,e??2,h,p)}function ge(t,e="reduced"){if(t.ndim!==2)throw new Error(`qr: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape,i=Math.min(h,p),u=z.zeros([h,p],"float64");for(let m=0;m<h;m++)for(let r=0;r<p;r++)u.set([m,r],Number(t.get(m,r)));const f=[],c=[];for(let m=0;m<i;m++){const r=h-m,a=[];for(let A=m;A<h;A++)a.push(Number(u.get(A,m)));let b=0;for(let A=0;A<r;A++)b+=a[A]*a[A];if(b=Math.sqrt(b),b<1e-15){f.push(a),c.push(0);continue}const y=a[0]>=0?1:-1,w=a[0]+y*b,d=[1];for(let A=1;A<r;A++)d.push(a[A]/w);const x=y*w/b;c.push(x),f.push(d);for(let A=m;A<p;A++){let S=0;for(let v=0;v<r;v++)S+=d[v]*Number(u.get(m+v,A));for(let v=0;v<r;v++)u.set([m+v,A],Number(u.get(m+v,A))-x*d[v]*S)}}if(e==="raw"){const m=z.zeros([h,p],"float64");for(let a=0;a<h;a++)for(let b=0;b<p;b++)m.set([a,b],Number(u.get(a,b)));const r=z.zeros([i],"float64");for(let a=0;a<i;a++)r.set([a],c[a]);return{h:m,tau:r}}if(e==="r"){const m=z.zeros([i,p],"float64");for(let r=0;r<i;r++)for(let a=r;a<p;a++)m.set([r,a],Number(u.get(r,a)));return m}const g=e==="complete"?h:i,o=z.zeros([h,g],"float64");for(let m=0;m<Math.min(h,g);m++)o.set([m,m],1);for(let m=i-1;m>=0;m--){const r=f[m],a=c[m],b=h-m;for(let y=m;y<g;y++){let w=0;for(let d=0;d<b;d++)w+=r[d]*Number(o.get(m+d,y));for(let d=0;d<b;d++)o.set([m+d,y],Number(o.get(m+d,y))-a*r[d]*w)}}const s=z.zeros([h,g],"float64");for(let m=0;m<h;m++)for(let r=0;r<g;r++)s.set([m,r],Number(o.get(m,r)));const l=e==="complete"?h:i,n=z.zeros([l,p],"float64");for(let m=0;m<l;m++)for(let r=0;r<p;r++)r>=m&&n.set([m,r],Number(u.get(m,r)));return{q:s,r:n}}function ke(t,e=!1){if(t.ndim!==2)throw new Error(`cholesky: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(h!==p)throw new Error(`cholesky: matrix must be square, got ${h}x${p}`);const i=h,u=z.zeros([i,i],"float64");for(let f=0;f<i;f++)for(let c=0;c<=f;c++){let g=0;if(f===c){for(let s=0;s<c;s++)g+=Number(u.get(c,s))**2;const o=Number(t.get(c,c))-g;if(o<0)throw new Error("cholesky: matrix is not positive definite");u.set([c,c],Math.sqrt(o))}else{for(let s=0;s<c;s++)g+=Number(u.get(f,s))*Number(u.get(c,s));const o=Number(u.get(c,c));if(Math.abs(o)<1e-15)throw new Error("cholesky: matrix is not positive definite");u.set([f,c],(Number(t.get(f,c))-g)/o)}}if(e){const f=z.zeros([i,i],"float64");for(let c=0;c<i;c++)for(let g=c;g<i;g++)f.set([c,g],Number(u.get(g,c)));return f}return u}function O(t){if(t.ndim!==2)throw new Error(`svd: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape,p=Math.min(e,h),i=z.zeros([h,h],"float64");for(let l=0;l<h;l++)for(let n=0;n<h;n++){let m=0;for(let r=0;r<e;r++)m+=Number(t.get(r,l))*Number(t.get(r,n));i.set([l,n],m)}const{values:u,vectors:f}=Z(i),c=Array.from({length:h},(l,n)=>n);c.sort((l,n)=>u[n]-u[l]);const g=z.zeros([p],"float64");for(let l=0;l<p;l++){const n=u[c[l]];g.set([l],Math.sqrt(Math.max(0,n)))}const o=z.zeros([h,h],"float64");for(let l=0;l<h;l++)for(let n=0;n<h;n++)o.set([l,n],f[n][c[l]]);const s=z.zeros([e,e],"float64");for(let l=0;l<e;l++)for(let n=0;n<p;n++){const m=Number(g.get(n));if(m>1e-10){let r=0;for(let a=0;a<h;a++)r+=Number(t.get(l,a))*Number(o.get(n,a));s.set([l,n],r/m)}}if(e>p)for(let l=p;l<e;l++){const n=new Array(e).fill(0);n[l]=1;for(let r=0;r<l;r++){let a=0;for(let b=0;b<e;b++)a+=n[b]*Number(s.get(b,r));for(let b=0;b<e;b++)n[b]=n[b]-a*Number(s.get(b,r))}let m=0;for(let r=0;r<e;r++)m+=n[r]*n[r];if(m=Math.sqrt(m),m>1e-10)for(let r=0;r<e;r++)s.set([r,l],n[r]/m)}return{u:s,s:g,vt:o}}function Z(t){const e=t.shape[0],h=100*e*e,p=1e-10,i=[];for(let c=0;c<e;c++){i.push([]);for(let g=0;g<e;g++)i[c].push(Number(t.get(c,g)))}const u=[];for(let c=0;c<e;c++){u.push([]);for(let g=0;g<e;g++)u[c].push(c===g?1:0)}for(let c=0;c<h;c++){let g=0,o=0,s=1;for(let d=0;d<e;d++)for(let x=d+1;x<e;x++)Math.abs(i[d][x])>g&&(g=Math.abs(i[d][x]),o=d,s=x);if(g<p)break;const l=i[o][o],n=i[s][s],m=i[o][s];let r;Math.abs(l-n)<1e-15?r=Math.PI/4:r=.5*Math.atan2(2*m,n-l);const a=Math.cos(r),b=Math.sin(r),y=a*a*l+b*b*n-2*b*a*m,w=b*b*l+a*a*n+2*b*a*m;i[o][o]=y,i[s][s]=w,i[o][s]=0,i[s][o]=0;for(let d=0;d<e;d++)if(d!==o&&d!==s){const x=i[d][o],A=i[d][s];i[d][o]=a*x-b*A,i[o][d]=i[d][o],i[d][s]=b*x+a*A,i[s][d]=i[d][s]}for(let d=0;d<e;d++){const x=u[d][o],A=u[d][s];u[d][o]=a*x-b*A,u[d][s]=b*x+a*A}}const f=[];for(let c=0;c<e;c++)f.push(i[c][c]);return{values:f,vectors:u}}function be(t,e=!0,h=!0){const p=O(t);if(!h)return p.s;if(!e){const[i,u]=t.shape,f=Math.min(i,u),c=z.zeros([i,f],"float64");for(let o=0;o<i;o++)for(let s=0;s<f;s++)c.set([o,s],Number(p.u.get(o,s)));const g=z.zeros([f,u],"float64");for(let o=0;o<f;o++)for(let s=0;s<u;s++)g.set([o,s],Number(p.vt.get(o,s)));return{u:c,s:p.s,vt:g}}return p}function Fe(t){if(t.ndim!==2)throw new Error(`det: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape;if(e!==h)throw new Error(`det: matrix must be square, got ${e}x${h}`);const p=e;if(p===0)return 1;const i=t.data;if(p===1)return Number(i[0]);if(p===2)return Number(i[0])*Number(i[3])-Number(i[1])*Number(i[2]);const{lu:u,sign:f}=Q(t),c=u.data;let g=f;for(let o=0;o<p;o++)g*=c[o*p+o];return g}function Q(t){const[e,h]=t.shape,p=e,i=h,u=z.zeros([p,i],"float64"),f=u.data,c=t.data;for(let s=0;s<p*i;s++)f[s]=Number(c[s]);const g=Array.from({length:p},(s,l)=>l);let o=1;for(let s=0;s<Math.min(p,i);s++){let l=Math.abs(f[s*i+s]),n=s;for(let r=s+1;r<p;r++){const a=Math.abs(f[r*i+s]);a>l&&(l=a,n=r)}if(n!==s){for(let a=0;a<i;a++){const b=f[s*i+a];f[s*i+a]=f[n*i+a],f[n*i+a]=b}const r=g[s];g[s]=g[n],g[n]=r,o=-o}const m=f[s*i+s];if(Math.abs(m)>1e-15)for(let r=s+1;r<p;r++){const a=f[r*i+s]/m;f[r*i+s]=a;for(let b=s+1;b<i;b++)f[r*i+b]=f[r*i+b]-a*f[s*i+b]}}return{lu:u,piv:g,sign:o}}function K(t){if(t.ndim!==2)throw new Error(`inv: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape;if(e!==h)throw new Error(`inv: matrix must be square, got ${e}x${h}`);const p=e,{lu:i,piv:u}=Q(t),f=i.data,c=z.zeros([p,p],"float64"),g=c.data;for(let o=0;o<p;o++){const s=new Float64Array(p);for(let l=0;l<p;l++){let n=u[l]===o?1:0;for(let m=0;m<l;m++)n-=f[l*p+m]*s[m];s[l]=n}for(let l=p-1;l>=0;l--){let n=s[l];for(let r=l+1;r<p;r++)n-=f[l*p+r]*g[r*p+o];const m=f[l*p+l];if(Math.abs(m)<1e-15)throw new Error("inv: singular matrix");g[l*p+o]=n/m}}return c}function oe(t,e){const[h]=t.shape,p=h,{lu:i,piv:u}=Q(t),f=i.data,c=e.data,g=new Float64Array(p);for(let n=0;n<p;n++)g[n]=Number(c[u[n]]);const o=new Float64Array(p);for(let n=0;n<p;n++){let m=g[n];for(let r=0;r<n;r++)m-=f[n*p+r]*o[r];o[n]=m}const s=z.zeros([p],"float64"),l=s.data;for(let n=p-1;n>=0;n--){let m=o[n];for(let a=n+1;a<p;a++)m-=f[n*p+a]*l[a];const r=f[n*p+n];if(Math.abs(r)<1e-15)throw new Error("solve: singular matrix");l[n]=m/r}return s}function de(t,e){if(t.ndim!==2)throw new Error(`solve: coefficient matrix must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(h!==p)throw new Error(`solve: coefficient matrix must be square, got ${h}x${p}`);const i=h;if(e.ndim===1){if(e.shape[0]!==i)throw new Error(`solve: incompatible shapes (${h},${p}) and (${e.shape[0]},)`);return oe(t,e)}if(e.ndim===2){if(e.shape[0]!==i)throw new Error(`solve: incompatible shapes (${h},${p}) and (${e.shape[0]},${e.shape[1]})`);const u=e.shape[1],f=z.zeros([i,u],"float64");for(let c=0;c<u;c++){const g=z.zeros([i],"float64");for(let s=0;s<i;s++)g.set([s],Number(e.get(s,c)));const o=oe(t,g);for(let s=0;s<i;s++)f.set([s,c],Number(o.get(s)))}return f}throw new Error(`solve: b must be 1D or 2D, got ${e.ndim}D`)}function qe(t,e,h=null){if(t.ndim!==2)throw new Error(`lstsq: coefficient matrix must be 2D, got ${t.ndim}D`);const[p,i]=t.shape,{u,s:f,vt:c}=O(t),g=Math.min(p,i),o=h??Math.max(p,i)*Number.EPSILON,l=Number(f.get(0))*o;let n=0;for(let w=0;w<g;w++)Number(f.get(w))>l&&n++;const m=e.ndim===1?R.reshape(e,[e.size,1]):e,r=m.shape[1];if(m.shape[0]!==p)throw new Error(`lstsq: incompatible shapes (${p},${i}) and (${e.shape.join(",")})`);const a=z.zeros([i,r],"float64");for(let w=0;w<r;w++){const d=new Array(p).fill(0);for(let x=0;x<p;x++)for(let A=0;A<p;A++)d[x]+=Number(u.get(A,x))*Number(m.get(A,w));for(let x=0;x<i;x++){let A=0;for(let S=0;S<g;S++){const v=Number(f.get(S));v>l&&(A+=Number(c.get(S,x))*d[S]/v)}a.set([x,w],A)}}let b;if(p>i){b=z.zeros([r],"float64");for(let w=0;w<r;w++){let d=0;for(let x=0;x<p;x++){let A=0;for(let v=0;v<i;v++)A+=Number(t.get(x,v))*Number(a.get(v,w));const S=A-Number(m.get(x,w));d+=S*S}b.set([w],d)}}else b=z.zeros([0],"float64");return{x:e.ndim===1?R.reshape(a,[i]):a,residuals:b,rank:n,s:f}}function Re(t,e=2){if(t.ndim!==2)throw new Error(`cond: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(e===2||e===-2){const{s:c}=O(t),g=Math.min(h,p),o=Number(c.get(0)),s=Number(c.get(g-1));return e===2?s>0?o/s:1/0:o>0?s/o:0}if(h!==p)throw new Error(`cond: matrix must be square for p=${e}`);const i=P(t,e),u=K(t),f=P(u,e);return i*f}function _e(t,e){if(t.ndim===0)return Number(t.get())!==0?1:0;if(t.ndim===1){for(let f=0;f<t.size;f++)if(Number(t.get(f))!==0)return 1;return 0}if(t.ndim!==2)throw new Error(`matrix_rank: input must be at most 2D, got ${t.ndim}D`);const{s:h}=O(t),p=Number(h.get(0)),i=e??p*Math.max(t.shape[0],t.shape[1])*Number.EPSILON;let u=0;for(let f=0;f<h.size;f++)Number(h.get(f))>i&&u++;return u}function Be(t,e){if(t.ndim!==2)throw new Error(`matrix_power: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(h!==p)throw new Error(`matrix_power: matrix must be square, got ${h}x${p}`);const i=h;if(!Number.isInteger(e))throw new Error("matrix_power: exponent must be an integer");if(e===0){const o=z.zeros([i,i],"float64");for(let s=0;s<i;s++)o.set([s,s],1);return o}let u=t,f=e;e<0&&(u=K(t),f=-e);let c=z.zeros([i,i],"float64");for(let o=0;o<i;o++)c.set([o,o],1);let g=z.zeros([i,i],"float64");for(let o=0;o<i;o++)for(let s=0;s<i;s++)g.set([o,s],Number(u.get(o,s)));for(;f>0;)f&1&&(c=B(c,g)),g=B(g,g),f>>=1;return c}function Te(t,e=1e-15){if(t.ndim!==2)throw new Error(`pinv: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape,{u:i,s:u,vt:f}=O(t),c=Math.min(h,p),o=Number(u.get(0))*e,s=z.zeros([p,h],"float64");for(let l=0;l<p;l++)for(let n=0;n<h;n++){let m=0;for(let r=0;r<c;r++){const a=Number(u.get(r));a>o&&(m+=Number(f.get(r,l))*Number(i.get(n,r))/a)}s.set([l,n],m)}return s}function ye(t){if(t.ndim!==2)throw new Error(`eig: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape;if(e!==h)throw new Error(`eig: matrix must be square, got ${e}x${h}`);const p=e;let i=!0;e:for(let s=0;s<p;s++)for(let l=s+1;l<p;l++)if(Math.abs(Number(t.get(s,l))-Number(t.get(l,s)))>1e-10){i=!1;break e}if(i){const{values:s,vectors:l}=Z(t),n=z.zeros([p],"float64"),m=z.zeros([p,p],"float64");for(let r=0;r<p;r++){n.set([r],s[r]);for(let a=0;a<p;a++)m.set([a,r],l[a][r])}return{w:n,v:m}}const{values:u,vectors:f,hasComplexEigenvalues:c}=we(t);c&&console.warn("numpy-ts: eig() detected complex eigenvalues which cannot be represented. Results are real approximations and may be inaccurate. For symmetric matrices, use eigh() instead.");const g=z.zeros([p],"float64"),o=z.zeros([p,p],"float64");for(let s=0;s<p;s++){g.set([s],u[s]);for(let l=0;l<p;l++)o.set([l,s],f[l][s])}return{w:g,v:o}}function we(t){const e=t.shape[0],h=1e3,p=1e-10;let i=z.zeros([e,e],"float64");for(let o=0;o<e;o++)for(let s=0;s<e;s++)i.set([o,s],Number(t.get(o,s)));let u=z.zeros([e,e],"float64");for(let o=0;o<e;o++)u.set([o,o],1);for(let o=0;o<h;o++){let s=0;for(let r=0;r<e;r++)for(let a=0;a<e;a++)r!==a&&(s+=Number(i.get(r,a))**2);if(Math.sqrt(s)<p*e)break;const l=ge(i,"reduced"),n=l.q,m=l.r;i=B(m,n),u=B(u,n)}let f=!1;for(let o=0;o<e-1;o++){const s=Math.abs(Number(i.get(o+1,o))),l=Math.abs(Number(i.get(o,o))),n=Math.abs(Number(i.get(o+1,o+1))),m=Math.max(l,n,1e-10);if(s/m>1e-6){f=!0;break}}const c=[];for(let o=0;o<e;o++)c.push(Number(i.get(o,o)));const g=[];for(let o=0;o<e;o++){g.push([]);for(let s=0;s<e;s++)g[o].push(Number(u.get(o,s)))}return{values:c,vectors:g,hasComplexEigenvalues:f}}function Ae(t,e="L"){if(t.ndim!==2)throw new Error(`eigh: input must be 2D, got ${t.ndim}D`);const[h,p]=t.shape;if(h!==p)throw new Error(`eigh: matrix must be square, got ${h}x${p}`);const i=h,u=z.zeros([i,i],"float64");for(let l=0;l<i;l++)for(let n=0;n<i;n++)e==="L"?l>=n&&(u.set([l,n],Number(t.get(l,n))),u.set([n,l],Number(t.get(l,n)))):n>=l&&(u.set([l,n],Number(t.get(l,n))),u.set([n,l],Number(t.get(l,n))));const{values:f,vectors:c}=Z(u),g=Array.from({length:i},(l,n)=>n);g.sort((l,n)=>f[l]-f[n]);const o=z.zeros([i],"float64"),s=z.zeros([i,i],"float64");for(let l=0;l<i;l++){o.set([l],f[g[l]]);for(let n=0;n<i;n++)s.set([n,l],c[n][g[l]])}return{w:o,v:s}}function Oe(t){const{w:e}=ye(t);return e}function Le(t,e="L"){const{w:h}=Ae(t,e);return h}function Pe(t,e){const h=R.flatten(t),p=R.flatten(e),i=h.shape[0],u=p.shape[0];if(i!==u)throw new Error(`vdot: arrays must have same number of elements, got ${i} and ${u}`);if(k(t.dtype)||k(e.dtype)){let g=0,o=0;for(let s=0;s<i;s++){const l=h.get(s),n=p.get(s),m=l instanceof I?l.re:Number(l),r=l instanceof I?-l.im:0,a=n instanceof I?n.re:Number(n),b=n instanceof I?n.im:0;g+=m*a+r*b,o+=m*b-r*a}return Math.abs(o)<1e-15?g:new I(g,o)}let c=0;for(let g=0;g<i;g++){const o=h.get(g),s=p.get(g);typeof o=="bigint"&&typeof s=="bigint"?c=(typeof c=="bigint"?c:BigInt(c))+o*s:c=(typeof c=="bigint"?Number(c):c)+Number(o)*Number(s)}return c}function Je(t,e,h=-1){const p=t.ndim,i=e.ndim,u=h<0?p+h:h,f=h<0?i+h:h;if(u<0||u>=p)throw new Error(`vecdot: axis ${h} out of bounds for array with ${p} dimensions`);if(f<0||f>=i)throw new Error(`vecdot: axis ${h} out of bounds for array with ${i} dimensions`);const c=t.shape[u],g=e.shape[f];if(c!==g)throw new Error(`vecdot: axis dimensions must match, got ${c} and ${g}`);if(p===1&&i===1)return X(t,e);const o=[...t.shape.slice(0,u),...t.shape.slice(u+1)],s=[...e.shape.slice(0,f),...e.shape.slice(f+1)],l=c,n=k(t.dtype)||k(e.dtype),m=V(t.dtype,e.dtype),r=o.length>s.length?o:s;if(r.length===0){let y=n?new I(0,0):0;for(let w=0;w<l;w++){const d=t.get(w),x=e.get(w),A=T(d,x);if(y instanceof I||A instanceof I){const S=y instanceof I?y:new I(Number(y),0),v=A instanceof I?A:new I(Number(A),0);y=S.add(v)}else typeof y=="bigint"||typeof A=="bigint"?y=BigInt(y)+BigInt(A):y=y+A}return y}const a=z.zeros(r,m),b=r.reduce((y,w)=>y*w,1);for(let y=0;y<b;y++){const w=[];let d=y;for(let v=r.length-1;v>=0;v--)w.unshift(d%r[v]),d=Math.floor(d/r[v]);const x=[...w.slice(0,u),0,...w.slice(u)],A=[...w.slice(0,f),0,...w.slice(f)];let S=n?new I(0,0):0;for(let v=0;v<l;v++){x[u]=v,A[f]=v;const N=t.get(...x),M=e.get(...A),$=T(N,M);if(S instanceof I||$ instanceof I){const D=S instanceof I?S:new I(Number(S),0),C=$ instanceof I?$:new I(Number($),0);S=D.add(C)}else typeof S=="bigint"||typeof $=="bigint"?S=BigInt(S)+BigInt($):S=S+$}a.set(w,S)}return a}function Xe(t){if(t.ndim<2)throw new Error(`matrix_transpose: input must have at least 2 dimensions, got ${t.ndim}D`);const e=Array.from({length:t.ndim},(p,i)=>i),h=e.length-1;return e[h]=h-1,e[h-1]=h,L(t,e)}function We(t,e){return L(t,e)}function Qe(t,e){if(t.ndim<2)throw new Error(`matvec: x1 must have at least 2 dimensions, got ${t.ndim}D`);if(e.ndim<1)throw new Error(`matvec: x2 must have at least 1 dimension, got ${e.ndim}D`);const h=t.shape[t.ndim-2],p=t.shape[t.ndim-1],i=e.shape[e.ndim-1];if(p!==i)throw new Error(`matvec: last axis of x1 (${p}) must match last axis of x2 (${i})`);if(t.ndim===2&&e.ndim===1)return X(t,e);const u=t.shape.slice(0,-2),f=e.shape.slice(0,-1),c=Math.max(u.length,f.length),g=[...Array(c-u.length).fill(1),...u],o=[...Array(c-f.length).fill(1),...f],s=[];for(let b=0;b<c;b++){const y=g[b],w=o[b];if(y!==1&&w!==1&&y!==w)throw new Error(`matvec: batch dimensions not broadcastable: ${u} vs ${f}`);s.push(Math.max(y,w))}const l=[...s,h],n=V(t.dtype,e.dtype),m=z.zeros(l,n),r=k(n),a=s.reduce((b,y)=>b*y,1);for(let b=0;b<a;b++){const y=[];let w=b;for(let A=s.length-1;A>=0;A--)y.unshift(w%s[A]),w=Math.floor(w/s[A]);const d=y.slice(-(u.length||1)).map((A,S)=>(u[S]??1)===1?0:A),x=y.slice(-(f.length||1)).map((A,S)=>(f[S]??1)===1?0:A);for(let A=0;A<h;A++){let S=r?new I(0,0):0;for(let N=0;N<p;N++){const M=[...d,A,N],$=[...x,N],D=t.get(...M),C=e.get(...$),E=T(D,C);if(S instanceof I||E instanceof I){const j=S instanceof I?S:new I(Number(S),0),F=E instanceof I?E:new I(Number(E),0);S=j.add(F)}else typeof S=="bigint"||typeof E=="bigint"?S=BigInt(S)+BigInt(E):S=S+E}const v=[...y,A];m.set(v,S)}}return m}function Ue(t,e){if(t.ndim<1)throw new Error(`vecmat: x1 must have at least 1 dimension, got ${t.ndim}D`);if(e.ndim<2)throw new Error(`vecmat: x2 must have at least 2 dimensions, got ${e.ndim}D`);const h=t.shape[t.ndim-1],p=e.shape[e.ndim-2],i=e.shape[e.ndim-1];if(h!==p)throw new Error(`vecmat: last axis of x1 (${h}) must match second-to-last axis of x2 (${p})`);if(t.ndim===1&&e.ndim===2)return X(t,e);const u=t.shape.slice(0,-1),f=e.shape.slice(0,-2),c=Math.max(u.length,f.length),g=[...Array(c-u.length).fill(1),...u],o=[...Array(c-f.length).fill(1),...f],s=[];for(let b=0;b<c;b++){const y=g[b],w=o[b];if(y!==1&&w!==1&&y!==w)throw new Error(`vecmat: batch dimensions not broadcastable: ${u} vs ${f}`);s.push(Math.max(y,w))}const l=[...s,i],n=V(t.dtype,e.dtype),m=z.zeros(l,n),r=k(n),a=s.reduce((b,y)=>b*y,1);for(let b=0;b<a;b++){const y=[];let w=b;for(let A=s.length-1;A>=0;A--)y.unshift(w%s[A]),w=Math.floor(w/s[A]);const d=y.slice(-(u.length||1)).map((A,S)=>(u[S]??1)===1?0:A),x=y.slice(-(f.length||1)).map((A,S)=>(f[S]??1)===1?0:A);for(let A=0;A<i;A++){let S=r?new I(0,0):0;for(let N=0;N<h;N++){const M=[...d,N],$=[...x,N,A],D=t.get(...M),C=e.get(...$),E=T(D,C);if(S instanceof I||E instanceof I){const j=S instanceof I?S:new I(Number(S),0),F=E instanceof I?E:new I(Number(E),0);S=j.add(F)}else typeof S=="bigint"||typeof E=="bigint"?S=BigInt(S)+BigInt(E):S=S+E}const v=[...y,A];m.set(v,S)}}return m}function Ge(t){if(t.ndim!==2)throw new Error(`slogdet: input must be 2D, got ${t.ndim}D`);const[e,h]=t.shape;if(e!==h)throw new Error(`slogdet: matrix must be square, got ${e}x${h}`);const p=e;if(p===0)return{sign:1,logabsdet:0};const{lu:i,sign:u}=Q(t),f=i.data;let c=0,g=u;for(let o=0;o<p;o++){const s=f[o*p+o];if(s===0)return{sign:0,logabsdet:-1/0};s<0&&(g=-g),c+=Math.log(Math.abs(s))}return{sign:g,logabsdet:c}}function He(t){return be(t,!0,!1)}function Ye(t){if(t.length<2)throw new Error("multi_dot: need at least 2 arrays");if(t.length===2)return B(t[0],t[1]);let e=t[0];for(let h=1;h<t.length;h++)e=B(e,t[h]);return e}function Ze(t,e=2){if(e<=0)throw new Error(`tensorinv: ind must be positive, got ${e}`);const h=t.shape,p=t.ndim;if(p<e)throw new Error(`tensorinv: array has ${p} dimensions, ind=${e} is too large`);let i=1;for(let o=0;o<e;o++)i*=h[o];let u=1;for(let o=e;o<p;o++)u*=h[o];if(i!==u)throw new Error(`tensorinv: product of first ${e} dimensions (${i}) must equal product of remaining dimensions (${u})`);const f=R.reshape(t,[i,u]),c=K(f),g=[...h.slice(e),...h.slice(0,e)];return R.reshape(c,g)}function Ke(t,e,h){const p=t.shape,i=e.shape,u=t.ndim,f=e.ndim;let c;h==null?c=Array.from({length:f},(d,x)=>u-f+x):c=h.map(d=>d<0?u+d:d);const g=[];for(let d=0;d<u;d++)c.includes(d)||g.push(d);const o=[...g,...c],s=L(t,o),n=c.map(d=>p[d]).reduce((d,x)=>d*x,1),r=g.map(d=>p[d]).reduce((d,x)=>d*x,1),a=i.reduce((d,x)=>d*x,1);if(n!==a)throw new Error(`tensorsolve: dimensions don't match - sum dimensions product (${n}) != b total elements (${a})`);if(r!==n)throw new Error(`tensorsolve: non-square problem - other dimensions product (${r}) != sum dimensions product (${n})`);const b=R.reshape(s,[r,n]),y=R.reshape(e,[n]),w=de(b,y);return R.reshape(w,[...i])}function et(t,...e){const h=t.indexOf("->");let p,i;h===-1?(p=t,i=ne(p)):(p=t.slice(0,h),i=t.slice(h+2));const u=p.split(",").map(n=>n.trim());if(u.length!==e.length)throw new Error(`einsum_path: expected ${u.length} operands, got ${e.length}`);const f=e.map(n=>Array.isArray(n)?n:Array.from(n.shape)),c=new Map;for(let n=0;n<e.length;n++){const m=u[n],r=f[n];if(m.length!==r.length)throw new Error(`einsum_path: operand ${n} has ${r.length} dimensions but subscript '${m}' has ${m.length} indices`);for(let a=0;a<m.length;a++){const b=m[a],y=r[a];if(c.has(b)&&c.get(b)!==y)throw new Error(`einsum_path: size mismatch for index '${b}': ${c.get(b)} vs ${y}`);c.set(b,y)}}if(e.length===1){const n=[[0]];return[n,ee(t,f,n,c)]}if(e.length===2){const n=[[0,1]];return[n,ee(t,f,n,c)]}const g=[],o=[...u],s=[...f],l=e.map((n,m)=>m);for(;o.length>1;){let n=0,m=1,r=1/0;for(let y=0;y<o.length;y++)for(let w=y+1;w<o.length;w++){const d=xe(o[y],o[w],s[y],s[w],i,c);d<r&&(r=d,n=y,m=w)}g.push([l[n],l[m]]);const[a,b]=Se(o[n],o[m],s[n],s[m],i,c);o.splice(m,1),o.splice(n,1),s.splice(m,1),s.splice(n,1),l.splice(m,1),l.splice(n,1),o.push(a),s.push(b),l.push(-1)}return[g,ee(t,f,g,c)]}function xe(t,e,h,p,i,u){const f=new Set(t),c=new Set(e);let g=1;for(const o of f)g*=u.get(o)||1;for(const o of c)f.has(o)||(g*=u.get(o)||1);return g}function Se(t,e,h,p,i,u){const f=new Set([...t,...e]),c=new Map;for(const l of t)c.set(l,(c.get(l)||0)+1);for(const l of e)c.set(l,(c.get(l)||0)+1);const g=new Set(i),o=[];for(const l of f)(g.has(l)||c.get(l)===1)&&o.push(l);o.sort();const s=o.map(l=>u.get(l));return[o.join(""),s]}function ee(t,e,h,p){const i=[];i.push(" Complete contraction: "+t),i.push(" Operand shapes: "+e.map(f=>`(${f.join(", ")})`).join(", ")),i.push(" Contraction path: "+JSON.stringify(h));let u=0;for(const f of e)u+=f.reduce((c,g)=>c*g,1);return i.push(" Estimated FLOPS: ~"+u.toExponential(2)),i.join(`
|
|
2
|
+
`)}export{ke as cholesky,Re as cond,je as cross,Fe as det,ze as diagonal,X as dot,ye as eig,Ae as eigh,Oe as eigvals,Le as eigvalsh,Ee as einsum,et as einsum_path,Me as inner,K as inv,Ce as kron,qe as lstsq,B as matmul,P as matrix_norm,Be as matrix_power,_e as matrix_rank,Xe as matrix_transpose,Qe as matvec,Ye as multi_dot,Ve as norm,pe as outer,We as permute_dims,Te as pinv,ge as qr,Ge as slogdet,de as solve,be as svd,He as svdvals,De as tensordot,Ze as tensorinv,Ke as tensorsolve,$e as trace,L as transpose,Pe as vdot,Je as vecdot,Ue as vecmat,W as vector_norm};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ArrayStorage as p}from"../storage";import{isBigIntDType as u,isComplexDType as b,throwIfComplex as N}from"../dtype";import{elementwiseComparisonOp as F}from"../internal/compute";import{broadcastShapes as I}from"../internal/compute";function D(t){return t!==0&&t!==0n}function g(t,o){const a=t[o*2],r=t[o*2+1];return a!==0||r!==0}function h(t,o){return t.isCContiguous&&o.isCContiguous&&t.shape.length===o.shape.length&&t.shape.every((a,r)=>a===o.shape[r])}function P(t,o){return typeof o=="number"?v(t,o):h(t,o)?x(t,o):F(t,o,(a,r)=>D(a)&&D(r))}function x(t,o){const a=new Uint8Array(t.size),r=t.data,n=o.data,e=t.offset,s=o.offset,f=t.size,i=u(t.dtype),c=u(o.dtype),y=b(t.dtype),m=b(o.dtype);if(y||m)for(let l=0;l<f;l++){const A=y?g(r,e+l):r[e+l]!==0,d=m?g(n,s+l):n[s+l]!==0;a[l]=A&&d?1:0}else if(i||c)for(let l=0;l<f;l++){const A=i?r[e+l]!==0n:r[e+l]!==0,d=c?n[s+l]!==0n:n[s+l]!==0;a[l]=A&&d?1:0}else for(let l=0;l<f;l++)a[l]=r[e+l]!==0&&n[s+l]!==0?1:0;return p.fromData(a,Array.from(t.shape),"bool")}function v(t,o){const a=new Uint8Array(t.size),r=o!==0,n=t.size;if(t.isCContiguous){const e=t.data,s=t.offset;if(b(t.dtype)){const f=e;for(let i=0;i<n;i++)a[i]=g(f,s+i)&&r?1:0}else if(u(t.dtype)){const f=e;for(let i=0;i<n;i++)a[i]=f[s+i]!==0n&&r?1:0}else if(s===0)for(let f=0;f<n;f++)a[f]=e[f]!==0&&r?1:0;else for(let f=0;f<n;f++)a[f]=e[s+f]!==0&&r?1:0}else if(b(t.dtype))for(let e=0;e<n;e++){const s=t.iget(e);a[e]=(s.re!==0||s.im!==0)&&r?1:0}else if(u(t.dtype))for(let e=0;e<n;e++)a[e]=t.iget(e)!==0n&&r?1:0;else for(let e=0;e<n;e++)a[e]=Number(t.iget(e))!==0&&r?1:0;return p.fromData(a,Array.from(t.shape),"bool")}function q(t,o){if(typeof o=="number")return B(t,o);if(o.size===1&&!b(o.dtype)&&t.dtype===o.dtype){const a=Number(o.iget(0));return B(t,a)}return h(t,o)?w(t,o):F(t,o,(a,r)=>D(a)||D(r))}function w(t,o){const a=new Uint8Array(t.size),r=t.data,n=o.data,e=t.offset,s=o.offset,f=t.size,i=u(t.dtype),c=u(o.dtype),y=b(t.dtype),m=b(o.dtype);if(y||m)for(let l=0;l<f;l++){const A=y?g(r,e+l):r[e+l]!==0,d=m?g(n,s+l):n[s+l]!==0;a[l]=A||d?1:0}else if(i||c)for(let l=0;l<f;l++){const A=i?r[e+l]!==0n:r[e+l]!==0,d=c?n[s+l]!==0n:n[s+l]!==0;a[l]=A||d?1:0}else for(let l=0;l<f;l++)a[l]=r[e+l]!==0||n[s+l]!==0?1:0;return p.fromData(a,Array.from(t.shape),"bool")}function B(t,o){const a=new Uint8Array(t.size),r=o!==0,n=t.size;if(t.isCContiguous){const e=t.data,s=t.offset;if(b(t.dtype)){const f=e;for(let i=0;i<n;i++)a[i]=g(f,s+i)||r?1:0}else if(u(t.dtype)){const f=e;for(let i=0;i<n;i++)a[i]=f[s+i]!==0n||r?1:0}else if(s===0)for(let f=0;f<n;f++)a[f]=e[f]!==0||r?1:0;else for(let f=0;f<n;f++)a[f]=e[s+f]!==0||r?1:0}else if(b(t.dtype))for(let e=0;e<n;e++){const s=t.iget(e);a[e]=s.re!==0||s.im!==0||r?1:0}else if(u(t.dtype))for(let e=0;e<n;e++)a[e]=t.iget(e)!==0n||r?1:0;else for(let e=0;e<n;e++)a[e]=Number(t.iget(e))!==0||r?1:0;return p.fromData(a,Array.from(t.shape),"bool")}function G(t){const o=new Uint8Array(t.size),a=t.size;if(t.isCContiguous){const r=t.data,n=t.offset;if(b(t.dtype)){const e=r;for(let s=0;s<a;s++)o[s]=g(e,n+s)?0:1}else if(u(t.dtype)){const e=r;for(let s=0;s<a;s++)o[s]=e[n+s]===0n?1:0}else if(n===0)for(let e=0;e<a;e++)o[e]=r[e]===0?1:0;else for(let e=0;e<a;e++)o[e]=r[n+e]===0?1:0}else if(b(t.dtype))for(let r=0;r<a;r++){const n=t.iget(r);o[r]=n.re===0&&n.im===0?1:0}else if(u(t.dtype))for(let r=0;r<a;r++)o[r]=t.iget(r)===0n?1:0;else for(let r=0;r<a;r++)o[r]=Number(t.iget(r))===0?1:0;return p.fromData(o,Array.from(t.shape),"bool")}function H(t,o){return typeof o=="number"?T(t,o):h(t,o)?U(t,o):F(t,o,(a,r)=>D(a)!==D(r))}function U(t,o){const a=new Uint8Array(t.size),r=t.data,n=o.data,e=t.offset,s=o.offset,f=t.size,i=u(t.dtype),c=u(o.dtype),y=b(t.dtype),m=b(o.dtype);if(y||m)for(let l=0;l<f;l++){const A=y?g(r,e+l):r[e+l]!==0,d=m?g(n,s+l):n[s+l]!==0;a[l]=A!==d?1:0}else if(i||c)for(let l=0;l<f;l++){const A=i?r[e+l]!==0n:r[e+l]!==0,d=c?n[s+l]!==0n:n[s+l]!==0;a[l]=A!==d?1:0}else for(let l=0;l<f;l++){const A=r[e+l]!==0,d=n[s+l]!==0;a[l]=A!==d?1:0}return p.fromData(a,Array.from(t.shape),"bool")}function T(t,o){const a=new Uint8Array(t.size),r=o!==0,n=t.size;if(t.isCContiguous){const e=t.data,s=t.offset;if(b(t.dtype)){const f=e;for(let i=0;i<n;i++){const c=g(f,s+i);a[i]=c!==r?1:0}}else if(u(t.dtype)){const f=e;for(let i=0;i<n;i++){const c=f[s+i]!==0n;a[i]=c!==r?1:0}}else if(s===0)for(let f=0;f<n;f++){const i=e[f]!==0;a[f]=i!==r?1:0}else for(let f=0;f<n;f++){const i=e[s+f]!==0;a[f]=i!==r?1:0}}else if(b(t.dtype))for(let e=0;e<n;e++){const s=t.iget(e),f=s.re!==0||s.im!==0;a[e]=f!==r?1:0}else if(u(t.dtype))for(let e=0;e<n;e++){const s=t.iget(e)!==0n;a[e]=s!==r?1:0}else for(let e=0;e<n;e++){const s=Number(t.iget(e))!==0;a[e]=s!==r?1:0}return p.fromData(a,Array.from(t.shape),"bool")}function J(t){const o=new Uint8Array(t.size),a=t.size;if(t.isCContiguous){const r=t.data,n=t.offset;if(b(t.dtype)){const e=r;for(let s=0;s<a;s++){const f=e[(n+s)*2],i=e[(n+s)*2+1];o[s]=Number.isFinite(f)&&Number.isFinite(i)?1:0}}else if(u(t.dtype))for(let e=0;e<a;e++)o[e]=1;else if(n===0)for(let e=0;e<a;e++)o[e]=Number.isFinite(r[e])?1:0;else for(let e=0;e<a;e++){const s=r[n+e];o[e]=Number.isFinite(s)?1:0}}else if(b(t.dtype))for(let r=0;r<a;r++){const n=t.iget(r);o[r]=Number.isFinite(n.re)&&Number.isFinite(n.im)?1:0}else if(u(t.dtype))for(let r=0;r<a;r++)o[r]=1;else for(let r=0;r<a;r++)o[r]=Number.isFinite(Number(t.iget(r)))?1:0;return p.fromData(o,Array.from(t.shape),"bool")}function K(t){const o=new Uint8Array(t.size),a=t.size;if(t.isCContiguous){const r=t.data,n=t.offset;if(b(t.dtype)){const e=r;for(let s=0;s<a;s++){const f=e[(n+s)*2],i=e[(n+s)*2+1],c=!Number.isFinite(f)&&!Number.isNaN(f),y=!Number.isFinite(i)&&!Number.isNaN(i);o[s]=c||y?1:0}}else if(u(t.dtype))for(let e=0;e<a;e++)o[e]=0;else if(n===0)for(let e=0;e<a;e++){const s=r[e];o[e]=!Number.isFinite(s)&&!Number.isNaN(s)?1:0}else for(let e=0;e<a;e++){const s=r[n+e];o[e]=!Number.isFinite(s)&&!Number.isNaN(s)?1:0}}else if(b(t.dtype))for(let r=0;r<a;r++){const n=t.iget(r),e=!Number.isFinite(n.re)&&!Number.isNaN(n.re),s=!Number.isFinite(n.im)&&!Number.isNaN(n.im);o[r]=e||s?1:0}else if(u(t.dtype))for(let r=0;r<a;r++)o[r]=0;else for(let r=0;r<a;r++){const n=Number(t.iget(r));o[r]=!Number.isFinite(n)&&!Number.isNaN(n)?1:0}return p.fromData(o,Array.from(t.shape),"bool")}function Q(t){const o=new Uint8Array(t.size),a=t.size;if(t.isCContiguous){const r=t.data,n=t.offset;if(b(t.dtype)){const e=r;for(let s=0;s<a;s++){const f=e[(n+s)*2],i=e[(n+s)*2+1];o[s]=Number.isNaN(f)||Number.isNaN(i)?1:0}}else if(u(t.dtype))for(let e=0;e<a;e++)o[e]=0;else for(let e=0;e<a;e++)o[e]=Number.isNaN(r[n+e])?1:0}else if(b(t.dtype))for(let r=0;r<a;r++){const n=t.iget(r);o[r]=Number.isNaN(n.re)||Number.isNaN(n.im)?1:0}else if(u(t.dtype))for(let r=0;r<a;r++)o[r]=0;else for(let r=0;r<a;r++)o[r]=Number.isNaN(Number(t.iget(r)))?1:0;return p.fromData(o,Array.from(t.shape),"bool")}function W(t){const o=new Uint8Array(t.size);return p.fromData(o,Array.from(t.shape),"bool")}function Y(t,o){if(N(t.dtype,"copysign","copysign is only defined for real numbers."),typeof o!="number"&&N(o.dtype,"copysign","copysign is only defined for real numbers."),typeof o=="number")return M(t,o);if(h(t,o))return V(t,o);const a=I(t.shape,o.shape),r=a.reduce((i,c)=>i*c,1),n=p.zeros(a,"float64"),e=n.data,s=z(t,a),f=z(o,a);for(let i=0;i<r;i++){const c=Number(s.iget(i)),y=Number(f.iget(i));e[i]=Math.sign(y)*Math.abs(c)}return n}function V(t,o){const a=p.zeros(Array.from(t.shape),"float64"),r=a.data,n=t.size,e=t.data,s=o.data,f=t.offset,i=o.offset,c=u(t.dtype),y=u(o.dtype);for(let m=0;m<n;m++){const l=c?Number(e[f+m]):e[f+m],A=y?Number(s[i+m]):s[i+m];r[m]=Math.sign(A)*Math.abs(l)}return a}function M(t,o){const a=p.zeros(Array.from(t.shape),"float64"),r=a.data,n=t.size,e=Math.sign(o);if(!t.isCContiguous){for(let i=0;i<n;i++)r[i]=e*Math.abs(Number(t.iget(i)));return a}const s=t.data,f=t.offset;if(u(t.dtype)){const i=s;for(let c=0;c<n;c++)r[c]=e*Math.abs(Number(i[f+c]))}else if(f===0)for(let i=0;i<n;i++)r[i]=e*Math.abs(s[i]);else for(let i=0;i<n;i++)r[i]=e*Math.abs(s[f+i]);return a}function Z(t){N(t.dtype,"signbit","signbit is only defined for real numbers.");const o=new Uint8Array(t.size),a=t.size;if(t.isCContiguous){const r=t.data,n=t.offset;if(u(t.dtype)){const e=r;for(let s=0;s<a;s++)o[s]=e[n+s]<0n?1:0}else for(let e=0;e<a;e++){const s=r[n+e];o[e]=s<0||Object.is(s,-0)?1:0}}else if(u(t.dtype))for(let r=0;r<a;r++)o[r]=t.iget(r)<0n?1:0;else for(let r=0;r<a;r++){const n=Number(t.iget(r));o[r]=n<0||Object.is(n,-0)?1:0}return p.fromData(o,Array.from(t.shape),"bool")}function $(t,o){if(N(t.dtype,"nextafter","nextafter is only defined for real numbers."),typeof o!="number"&&N(o.dtype,"nextafter","nextafter is only defined for real numbers."),typeof o=="number")return _(t,o);if(h(t,o))return O(t,o);const a=I(t.shape,o.shape),r=a.reduce((i,c)=>i*c,1),n=p.zeros(a,"float64"),e=n.data,s=z(t,a),f=z(o,a);for(let i=0;i<r;i++){const c=Number(s.iget(i)),y=Number(f.iget(i));e[i]=S(c,y)}return n}function O(t,o){const a=p.zeros(Array.from(t.shape),"float64"),r=a.data,n=t.size,e=t.data,s=o.data,f=t.offset,i=o.offset,c=u(t.dtype),y=u(o.dtype);for(let m=0;m<n;m++){const l=c?Number(e[f+m]):e[f+m],A=y?Number(s[i+m]):s[i+m];r[m]=S(l,A)}return a}function _(t,o){const a=p.zeros(Array.from(t.shape),"float64"),r=a.data,n=t.size;if(t.isCContiguous){const e=t.data,s=t.offset;if(u(t.dtype)){const f=e;for(let i=0;i<n;i++)r[i]=S(Number(f[s+i]),o)}else for(let f=0;f<n;f++)r[f]=S(e[s+f],o)}else for(let e=0;e<n;e++)r[e]=S(Number(t.iget(e)),o);return a}function S(t,o){if(Number.isNaN(t)||Number.isNaN(o))return NaN;if(t===o)return o;if(t===0)return o>0?Number.MIN_VALUE:-Number.MIN_VALUE;const a=new ArrayBuffer(8),r=new Float64Array(a),n=new BigInt64Array(a);r[0]=t;let e=n[0];return t>0&&o>t||t<0&&o>t?e=e+1n:e=e-1n,n[0]=e,r[0]}function tt(t){N(t.dtype,"spacing","spacing is only defined for real numbers.");const o=p.zeros(Array.from(t.shape),"float64"),a=o.data,r=t.size;if(t.isCContiguous){const n=t.data,e=t.offset;if(u(t.dtype)){const s=n;for(let f=0;f<r;f++)a[f]=C(Number(s[e+f]))}else for(let s=0;s<r;s++)a[s]=C(n[e+s])}else for(let n=0;n<r;n++)a[n]=C(Number(t.iget(n)));return o}function C(t){if(Number.isNaN(t))return NaN;if(!Number.isFinite(t))return NaN;if(Math.abs(t)===0)return Number.MIN_VALUE;const a=S(t,1/0);return Math.abs(a-t)}function z(t,o){const a=t.shape.length,r=o.length,n=new Array(r).fill(0);for(let e=0;e<a;e++){const s=r-a+e,f=t.shape[e],i=o[s];if(f===i)n[s]=t.strides[e];else if(f===1)n[s]=0;else throw new Error("Invalid broadcast")}return p.fromData(t.data,Array.from(o),t.dtype,n,t.offset)}function et(t){const o=t.dtype,a=t.size,r=new Uint8Array(a);if(b(o))if(t.isCContiguous){const n=t.offset,e=t.data;for(let s=0;s<a;s++)r[s]=e[(n+s)*2+1]!==0?1:0}else for(let n=0;n<a;n++){const e=t.iget(n);r[n]=e.im!==0?1:0}return p.fromData(r,Array.from(t.shape),"bool")}function rt(t){return b(t.dtype)}function ot(t){const o=t.dtype,a=t.size,r=new Uint8Array(a);if(b(o))if(t.isCContiguous){const n=t.offset,e=t.data;for(let s=0;s<a;s++)r[s]=e[(n+s)*2+1]===0?1:0}else for(let n=0;n<a;n++){const e=t.iget(n);r[n]=e.im===0?1:0}else r.fill(1);return p.fromData(r,Array.from(t.shape),"bool")}function at(t){return!b(t.dtype)}function st(t){N(t.dtype,"isneginf","This operation is not supported for complex values because it would be ambiguous.");const o=new Uint8Array(t.size),a=t.size;if(!u(t.dtype))if(t.isCContiguous){const r=t.data,n=t.offset;for(let e=0;e<a;e++){const s=r[n+e];o[e]=s===-1/0?1:0}}else for(let r=0;r<a;r++)o[r]=Number(t.iget(r))===-1/0?1:0;return p.fromData(o,Array.from(t.shape),"bool")}function nt(t){N(t.dtype,"isposinf","This operation is not supported for complex values because it would be ambiguous.");const o=new Uint8Array(t.size),a=t.size;if(!u(t.dtype))if(t.isCContiguous){const r=t.data,n=t.offset;for(let e=0;e<a;e++){const s=r[n+e];o[e]=s===1/0?1:0}}else for(let r=0;r<a;r++)o[r]=Number(t.iget(r))===1/0?1:0;return p.fromData(o,Array.from(t.shape),"bool")}function it(t){return t.isFContiguous}function ft(t,o=100){const a=t.dtype;if(b(a)){const r=t.size,e=o*(a==="complex64"?11920929e-14:2220446049250313e-31);if(t.isCContiguous){const s=t.data,f=t.offset;let i=!0;for(let c=0;c<r;c++){const y=s[(f+c)*2+1];if(Math.abs(y)>e){i=!1;break}}if(i){const c=a==="complex64"?"float32":"float64",y=p.zeros(Array.from(t.shape),c),m=y.data;for(let l=0;l<r;l++)m[l]=s[(f+l)*2];return y}}else{let s=!0;for(let f=0;f<r;f++){const i=t.iget(f);if(Math.abs(i.im)>e){s=!1;break}}if(s){const f=a==="complex64"?"float32":"float64",i=p.zeros(Array.from(t.shape),f),c=i.data;for(let y=0;y<r;y++){const m=t.iget(y);c[y]=m.re}return i}}return t.copy()}return t.copy()}function lt(t){return typeof t=="number"||typeof t=="bigint"||typeof t=="boolean"||typeof t=="string"}function ct(t){return t==null?!1:typeof t[Symbol.iterator]=="function"}function ut(t,o){const r={b:["bool"],i:["int8","int16","int32","int64"],u:["uint8","uint16","uint32","uint64"],f:["float32","float64"]}[o];return r?r.includes(t):!1}function yt(t,o){const a=["float64","float32","int64","int32","int16","int8","uint64","uint32","uint16","uint8","bool"],r=a.indexOf(t),n=a.indexOf(o);return r<=n?t:o}export{Y as copysign,et as iscomplex,rt as iscomplexobj,ut as isdtype,J as isfinite,it as isfortran,K as isinf,Q as isnan,W as isnat,st as isneginf,nt as isposinf,ot as isreal,at as isrealobj,lt as isscalar,ct as iterable,P as logical_and,G as logical_not,q as logical_or,H as logical_xor,$ as nextafter,yt as promote_types,ft as real_if_close,Z as signbit,tt as spacing};
|