numpy-ts 0.9.0 → 0.11.0

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.
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Complex number class for numpy-ts
3
+ *
4
+ * Represents complex numbers in JavaScript, similar to Python's complex type.
5
+ * Used when converting complex arrays to JavaScript values via toArray().
6
+ */
7
+ /**
8
+ * Represents a complex number with real and imaginary parts.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const z = new Complex(1, 2); // 1 + 2i
13
+ * console.log(z.re); // 1
14
+ * console.log(z.im); // 2
15
+ * console.log(z.toString()); // "(1+2j)"
16
+ * ```
17
+ */
18
+ export declare class Complex {
19
+ /** Real part */
20
+ readonly re: number;
21
+ /** Imaginary part */
22
+ readonly im: number;
23
+ constructor(re: number, im?: number);
24
+ /**
25
+ * Returns the magnitude (absolute value) of the complex number.
26
+ * |z| = sqrt(re² + im²)
27
+ */
28
+ abs(): number;
29
+ /**
30
+ * Returns the phase angle (argument) of the complex number in radians.
31
+ * arg(z) = atan2(im, re)
32
+ */
33
+ angle(): number;
34
+ /**
35
+ * Returns the complex conjugate.
36
+ * conj(a + bi) = a - bi
37
+ */
38
+ conj(): Complex;
39
+ /**
40
+ * Add another complex number or real number.
41
+ */
42
+ add(other: Complex | number): Complex;
43
+ /**
44
+ * Subtract another complex number or real number.
45
+ */
46
+ sub(other: Complex | number): Complex;
47
+ /**
48
+ * Multiply by another complex number or real number.
49
+ * (a + bi)(c + di) = (ac - bd) + (ad + bc)i
50
+ */
51
+ mul(other: Complex | number): Complex;
52
+ /**
53
+ * Divide by another complex number or real number.
54
+ * (a + bi) / (c + di) = ((ac + bd) + (bc - ad)i) / (c² + d²)
55
+ */
56
+ div(other: Complex | number): Complex;
57
+ /**
58
+ * Returns the negation of this complex number.
59
+ */
60
+ neg(): Complex;
61
+ /**
62
+ * Check equality with another complex number.
63
+ */
64
+ equals(other: Complex): boolean;
65
+ /**
66
+ * String representation matching NumPy/Python format: "(a+bj)"
67
+ */
68
+ toString(): string;
69
+ /**
70
+ * Create a Complex from various input formats.
71
+ * Accepts:
72
+ * - Complex instance
73
+ * - {re, im} object
74
+ * - [re, im] array
75
+ * - number (creates re + 0i)
76
+ */
77
+ static from(value: ComplexInput): Complex;
78
+ /**
79
+ * Check if a value is a complex number representation.
80
+ */
81
+ static isComplex(value: unknown): value is ComplexInput;
82
+ }
83
+ /**
84
+ * Input types that can be converted to Complex.
85
+ */
86
+ export type ComplexInput = Complex | {
87
+ re: number;
88
+ im?: number;
89
+ } | [number, number] | number;
90
+ /**
91
+ * Helper to check if a value looks like a complex number input.
92
+ */
93
+ export declare function isComplexLike(value: unknown): value is ComplexInput;
94
+ //# sourceMappingURL=complex.d.ts.map
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * Supports NumPy numeric types:
5
5
  * - Floating point: float32, float64
6
+ * - Complex: complex64, complex128
6
7
  * - Signed integers: int8, int16, int32, int64
7
8
  * - Unsigned integers: uint8, uint16, uint32, uint64
8
9
  * - Boolean: bool
@@ -10,7 +11,7 @@
10
11
  /**
11
12
  * All supported dtypes
12
13
  */
13
- export type DType = 'float64' | 'float32' | 'int64' | 'int32' | 'int16' | 'int8' | 'uint64' | 'uint32' | 'uint16' | 'uint8' | 'bool';
14
+ export type DType = 'float64' | 'float32' | 'complex128' | 'complex64' | 'int64' | 'int32' | 'int16' | 'int8' | 'uint64' | 'uint32' | 'uint16' | 'uint8' | 'bool';
14
15
  /**
15
16
  * TypedArray types for each dtype
16
17
  */
@@ -20,12 +21,16 @@ export type TypedArray = Float64Array | Float32Array | BigInt64Array | Int32Arra
20
21
  */
21
22
  export declare const DEFAULT_DTYPE: DType;
22
23
  /**
23
- * Get the TypedArray constructor for a given dtype
24
+ * Get the TypedArray constructor for a given dtype.
25
+ * For complex types, returns the underlying float array constructor.
26
+ * complex128 uses Float64Array, complex64 uses Float32Array.
27
+ * Note: Complex arrays have 2x the physical elements (interleaved real, imag).
24
28
  */
25
29
  export declare function getTypedArrayConstructor(dtype: DType): TypedArrayConstructor | null;
26
30
  type TypedArrayConstructor = Float64ArrayConstructor | Float32ArrayConstructor | BigInt64ArrayConstructor | Int32ArrayConstructor | Int16ArrayConstructor | Int8ArrayConstructor | BigUint64ArrayConstructor | Uint32ArrayConstructor | Uint16ArrayConstructor | Uint8ArrayConstructor;
27
31
  /**
28
- * Get the element size in bytes for a given dtype
32
+ * Get the element size in bytes for a given dtype.
33
+ * For complex types, returns the full element size (both real and imag parts).
29
34
  */
30
35
  export declare function getDTypeSize(dtype: DType): number;
31
36
  /**
@@ -40,6 +45,60 @@ export declare function isFloatDType(dtype: DType): boolean;
40
45
  * Check if dtype uses BigInt
41
46
  */
42
47
  export declare function isBigIntDType(dtype: DType): boolean;
48
+ /**
49
+ * Check if dtype is complex
50
+ */
51
+ export declare function isComplexDType(dtype: DType): boolean;
52
+ /**
53
+ * Throw a TypeError if the dtype is complex.
54
+ * Use this at the start of functions that don't support complex numbers.
55
+ *
56
+ * @param dtype - The dtype to check
57
+ * @param functionName - The name of the function (for error message)
58
+ * @param reason - Optional reason why complex is not supported
59
+ * @throws TypeError if dtype is complex
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * export function floor(storage: ArrayStorage): ArrayStorage {
64
+ * throwIfComplex(storage.dtype, 'floor', 'rounding is not defined for complex numbers');
65
+ * // ... rest of implementation
66
+ * }
67
+ * ```
68
+ */
69
+ export declare function throwIfComplex(dtype: DType, functionName: string, reason?: string): void;
70
+ /**
71
+ * Throw an error if the dtype is complex and the function doesn't yet support it.
72
+ * Use this for functions that SHOULD support complex but haven't been implemented yet.
73
+ *
74
+ * @param dtype - The dtype to check
75
+ * @param functionName - The name of the function (for error message)
76
+ * @throws Error if dtype is complex
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * export function sin(storage: ArrayStorage): ArrayStorage {
81
+ * throwIfComplexNotImplemented(storage.dtype, 'sin');
82
+ * // ... existing real-only implementation
83
+ * }
84
+ * ```
85
+ */
86
+ export declare function throwIfComplexNotImplemented(dtype: DType, functionName: string): void;
87
+ /**
88
+ * Get the underlying float dtype for a complex dtype.
89
+ * complex128 -> float64, complex64 -> float32
90
+ */
91
+ export declare function getComplexComponentDType(dtype: DType): 'float64' | 'float32';
92
+ /**
93
+ * Get the complex dtype for a given float component dtype.
94
+ * float64 -> complex128, float32 -> complex64
95
+ */
96
+ export declare function getComplexDType(componentDtype: 'float64' | 'float32'): 'complex128' | 'complex64';
97
+ /**
98
+ * Check if a value looks like a complex number input.
99
+ * Accepts: Complex instance, {re, im} object
100
+ */
101
+ export declare function isComplexLike(value: unknown): boolean;
43
102
  /**
44
103
  * Infer dtype from JavaScript value
45
104
  */