tensor.d.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. import type { DeepImmutable, Flatten, FloatArray, Length, Tuple } from "../types";
  2. /**
  3. * Computes the tensor dimension of a multi-dimensional array
  4. */
  5. export type Dimension<T> = T extends Array<infer U> ? [Length<T>, ...Dimension<U>] : T extends readonly [infer U, ...infer R] ? [Length<T>, ...Dimension<U>] : [];
  6. /**
  7. * Possible values for a Tensor
  8. */
  9. export type TensorValue = number[] | TensorValue[];
  10. /**
  11. * Extracts the value type of a Tensor
  12. */
  13. export type ValueOfTensor<T = unknown> = T extends Tensor<infer V> ? V : TensorValue;
  14. type TensorNumberArray<V extends TensorValue> = Length<Dimension<V>> extends 2 ? Tuple<number, 16> : V;
  15. /**
  16. * Describes a mathematical tensor.
  17. * @see https://wikipedia.org/wiki/Tensor
  18. */
  19. export interface Tensor<V extends TensorValue = TensorValue> {
  20. /**
  21. * An array of the size of each dimension.
  22. * For example, [3] for a Vector3 and [4,4] for a Matrix
  23. * @remarks
  24. * This is to allow implementations with using a getter
  25. */
  26. readonly dimension: Readonly<Dimension<V>>;
  27. /**
  28. * The rank of the tensor. This is the same as the length of the tensor's dimension array.
  29. * @remarks
  30. * This is to allow implementations with using a getter
  31. */
  32. readonly rank: number;
  33. /**
  34. * Gets class name
  35. * @returns the class name
  36. */
  37. getClassName(): string;
  38. /**
  39. * Gets current instance hash code
  40. * @returns the instance hash code as a number
  41. */
  42. getHashCode(): number;
  43. /**
  44. * Sets the instance coordinates in the given array from the given index.
  45. * @param array defines the source array
  46. * @param index defines the offset in source array
  47. * @returns the current instance
  48. */
  49. toArray(array: FloatArray, index?: number): this;
  50. /**
  51. * Update the current instance from an array
  52. * @param array defines the destination array
  53. * @param index defines the offset in the destination array
  54. * @returns the current instance
  55. */
  56. fromArray(array: DeepImmutable<FloatArray>, index?: number): this;
  57. /**
  58. * Copy the current instance to an array
  59. * @returns a new array with the instance coordinates.
  60. */
  61. asArray(): TensorNumberArray<V>;
  62. /**
  63. * Sets the current instance coordinates with the given source coordinates
  64. * @param source defines the source instance
  65. * @returns the current updated instance
  66. */
  67. copyFrom(source: DeepImmutable<this>): this;
  68. /**
  69. * Sets the instance coordinates with the given floats
  70. * @returns the current updated instance
  71. */
  72. copyFromFloats(...floats: TensorNumberArray<V>): this;
  73. /**
  74. * Sets the instance coordinates with the given floats
  75. * @returns the current updated instance
  76. */
  77. set(...values: TensorNumberArray<V>): this;
  78. /**
  79. * Sets the instance coordinates to the given value
  80. * @returns the current updated instance
  81. */
  82. setAll(value: number): this;
  83. /**
  84. * Add another instance with the current one
  85. * @param other defines the other instance
  86. * @returns a new instance set with the addition of the current instance and the given one coordinates
  87. */
  88. add(other: DeepImmutable<this>): this;
  89. /**
  90. * Sets the "result" coordinates with the addition of the current instance and the given one coordinates
  91. * @param other defines the other instance
  92. * @param result defines the target instance
  93. * @returns result input
  94. */
  95. addToRef(other: DeepImmutable<this>, result: this): this;
  96. /**
  97. * Set the instance coordinates by adding the given instance coordinates
  98. * @param other defines the other instance
  99. * @returns the current updated instance
  100. */
  101. addInPlace(other: DeepImmutable<this>): this;
  102. /**
  103. * Adds the given coordinates to the current instance
  104. * @param floats the floats to add
  105. * @returns the current updated instance
  106. */
  107. addInPlaceFromFloats(...floats: TensorNumberArray<V>): this;
  108. /**
  109. * Returns a new instance set with the subtracted coordinates of other's coordinates from the current coordinates.
  110. * @param other defines the other instance
  111. * @returns a new instance
  112. */
  113. subtract(other: DeepImmutable<this>): this;
  114. /**
  115. * Sets the "result" coordinates with the subtraction of the other's coordinates from the current coordinates.
  116. * @param other defines the other instance
  117. * @param result defines the target instance
  118. * @returns result input
  119. */
  120. subtractToRef(other: DeepImmutable<this>, result: this): this;
  121. /**
  122. * Sets the current instance coordinates by subtracting from it the given one coordinates
  123. * @param other defines the other instance
  124. * @returns the current updated instance
  125. */
  126. subtractInPlace(other: DeepImmutable<this>): this;
  127. /**
  128. * Returns a new instance set with the subtraction of the given floats from the current instance coordinates
  129. * @param floats the coordinates to subtract
  130. * @returns the resulting instance
  131. */
  132. subtractFromFloats(...floats: TensorNumberArray<V>): this;
  133. /**
  134. * Subtracts the given floats from the current instance coordinates and set the given instance "result" with this result
  135. * Note: Implementation uses array magic so types may be confusing.
  136. * @param args the coordinates to subtract with the last element as the result
  137. * @returns the result
  138. */
  139. subtractFromFloatsToRef(...args: [...TensorNumberArray<V>, this]): this;
  140. /**
  141. * Returns a new instance set with the multiplication of the current instance and the given one coordinates
  142. * @param other defines the other instance
  143. * @returns a new instance
  144. */
  145. multiply(other: DeepImmutable<this>): this;
  146. /**
  147. * Sets "result" coordinates with the multiplication of the current instance and the given one coordinates
  148. * @param other defines the other instance
  149. * @param result defines the target instance
  150. * @returns result input
  151. */
  152. multiplyToRef(other: DeepImmutable<this>, result: this): this;
  153. /**
  154. * Multiplies in place the current instance coordinates by the given ones
  155. * @param other defines the other instance
  156. * @returns the current updated instance
  157. */
  158. multiplyInPlace(other: DeepImmutable<this>): this;
  159. /**
  160. * Gets a new instance set with the instance coordinates multiplied by the given floats
  161. * @returns a new instance
  162. */
  163. multiplyByFloats(...floats: TensorNumberArray<V>): this;
  164. /**
  165. * Returns a new instance set with the instance coordinates divided by the given one coordinates
  166. * @param other defines the other instance
  167. * @returns a new instance
  168. */
  169. divide(other: DeepImmutable<this>): this;
  170. /**
  171. * Sets the "result" coordinates with the instance coordinates divided by the given one coordinates
  172. * @param other defines the other instance
  173. * @param result defines the target instance
  174. * @returns result input
  175. */
  176. divideToRef(other: DeepImmutable<this>, result: this): this;
  177. /**
  178. * Divides the current instance coordinates by the given ones
  179. * @param other defines the other instance
  180. * @returns the current updated instance
  181. */
  182. divideInPlace(other: DeepImmutable<this>): this;
  183. /**
  184. * Updates the current instance with the minmal coordinate values between its and the given instance ones.
  185. * @param other defines the other instance
  186. * @returns this current updated instance
  187. */
  188. minimizeInPlace(other: DeepImmutable<this>): this;
  189. /**
  190. * Updates the current instance with the minmal coordinate values between its and the given floats.
  191. * @param floats defines the floats to compare against
  192. * @returns this current updated instance
  193. */
  194. minimizeInPlaceFromFloats(...floats: TensorNumberArray<V>): this;
  195. /**
  196. * Updates the current instance with the maximal coordinate values between its and the given instance ones.
  197. * @param other defines the other instance
  198. * @returns this current updated instance
  199. */
  200. maximizeInPlace(other: DeepImmutable<this>): this;
  201. /**
  202. * Updates the current instance with the maximal coordinate values between its and the given floats.
  203. * @param floats defines the floats to compare against
  204. * @returns this current updated instance
  205. */
  206. maximizeInPlaceFromFloats(...floats: TensorNumberArray<V>): this;
  207. /**
  208. * Gets a new instance with current instance negated coordinates
  209. * @returns a new instance
  210. */
  211. negate(): this;
  212. /**
  213. * Negate this instance in place
  214. * @returns this
  215. */
  216. negateInPlace(): this;
  217. /**
  218. * Negate the current instance and stores the result in the given instance "result" coordinates
  219. * @param result defines the instance object where to store the result
  220. * @returns the result
  221. */
  222. negateToRef(result: this): this;
  223. /**
  224. * Multiply the instance coordinates by
  225. * @param scale defines the scaling factor
  226. * @returns the current updated instance
  227. */
  228. scaleInPlace(scale: number): this;
  229. /**
  230. * Returns a new instance scaled by "scale" from the current instance
  231. * @param scale defines the scaling factor
  232. * @returns a new instance
  233. */
  234. scale(scale: number): this;
  235. /**
  236. * Scale the current instance values by a factor to a given instance
  237. * @param scale defines the scale factor
  238. * @param result defines the instance object where to store the result
  239. * @returns result input
  240. */
  241. scaleToRef(scale: number, result: this): this;
  242. /**
  243. * Scale the current instance values by a factor and add the result to a given instance
  244. * @param scale defines the scale factor
  245. * @param result defines the instance object where to store the result
  246. * @returns result input
  247. */
  248. scaleAndAddToRef(scale: number, result: this): this;
  249. /**
  250. * Gets a boolean if two instances are equals
  251. * @param other defines the other instance
  252. * @returns true if the given instance coordinates strictly equal the current instance ones
  253. */
  254. equals(other: DeepImmutable<this>): boolean;
  255. /**
  256. * Gets a boolean if two instances are equals (using an epsilon value)
  257. * @param other defines the other instance
  258. * @param epsilon defines the minimal distance to consider equality
  259. * @returns true if the given instance coordinates are close to the current ones by a distance of epsilon.
  260. */
  261. equalsWithEpsilon(other: DeepImmutable<this>, epsilon?: number): boolean;
  262. /**
  263. * Returns true if the current Vectoe coordinates equals the given floats
  264. * @param floats defines the coordinates to compare against
  265. * @returns true if both instances are equal
  266. */
  267. equalsToFloats(...floats: TensorNumberArray<V>): boolean;
  268. /**
  269. * Gets a new instance from current instance floored values
  270. * eg (1.2, 2.31) returns (1, 2)
  271. * @returns a new instance
  272. */
  273. floor(): this;
  274. /**
  275. * Gets the current instance's floored values and stores them in result
  276. * @param result the instance to store the result in
  277. * @returns the result instance
  278. */
  279. floorToRef(result: this): this;
  280. /**
  281. * Gets a new instance from current instance fractional values
  282. * eg (1.2, 2.31) returns (0.2, 0.31)
  283. * @returns a new instance
  284. */
  285. fract(): this;
  286. /**
  287. * Gets the current instance's fractional values and stores them in result
  288. * @param result the instance to store the result in
  289. * @returns the result instance
  290. */
  291. fractToRef(result: this): this;
  292. /**
  293. * Gets a new instance copied from the instance
  294. * @returns a new instance
  295. */
  296. clone(): this;
  297. }
  298. /**
  299. * Static side of Tensor
  300. */
  301. export interface TensorStatic<T extends Tensor<any[]>> {
  302. /**
  303. * Creates a new instance from the given coordinates
  304. */
  305. new (...coords: Flatten<ValueOfTensor<T>>): T;
  306. /**
  307. * So [[static]].prototype has typings, instead of just any
  308. */
  309. prototype: T;
  310. /**
  311. * Returns a new instance with random values between min and max
  312. * @param min the minimum random value
  313. * @param max the maximum random value
  314. * @returns a instance with random values between min and max
  315. */
  316. Random(min?: number, max?: number): T;
  317. /**
  318. * Returns a new instance with random values between min and max
  319. * @param min the minimum random value
  320. * @param max the maximum random value
  321. * @param result the result to store the random values in
  322. * @returns the updated result instance
  323. */
  324. RandomToRef(min: number | undefined, max: number | undefined, result: T): T;
  325. /**
  326. * Gets a new instance from the given index element of the given array
  327. * @param array defines the data source
  328. * @param offset defines the offset in the data source
  329. * @returns a new instance
  330. */
  331. FromArray(array: DeepImmutable<FloatArray>, offset?: number): T;
  332. /**
  333. * Sets "result" from the given index element of the given array
  334. * @param array defines the data source
  335. * @param offset defines the offset in the data source
  336. * @param result defines the target instance
  337. * @returns result input
  338. */
  339. FromArrayToRef(array: DeepImmutable<FloatArray>, offset: number, result: T): T;
  340. /**
  341. * Sets the given instance "result" with the given floats.
  342. * @param args defines the coordinates of the source with the last paramater being the result
  343. */
  344. FromFloatsToRef(...args: [...Flatten<ValueOfTensor<T>>, T]): T;
  345. /**
  346. * Gets the dot product of the instance "left" and the instance "right"
  347. * @param left defines first instance
  348. * @param right defines second instance
  349. * @returns the dot product (float)
  350. */
  351. Dot(left: DeepImmutable<T>, right: DeepImmutable<T>): number;
  352. /**
  353. * Gets a new instance set with the minimal coordinate values from the "left" and "right" instances
  354. * @param left defines 1st instance
  355. * @param right defines 2nd instance
  356. * @returns a new instance
  357. */
  358. Minimize(left: DeepImmutable<T>, right: DeepImmutable<T>): T;
  359. /**
  360. * Gets a new instance set with the maximal coordinate values from the "left" and "right" instances
  361. * @param left defines 1st instance
  362. * @param right defines 2nd instance
  363. * @returns a new instance
  364. */
  365. Maximize(left: DeepImmutable<T>, right: DeepImmutable<T>): T;
  366. /**
  367. * Gets the distance between the instances "value1" and "value2"
  368. * @param value1 defines first instance
  369. * @param value2 defines second instance
  370. * @returns the distance between instances
  371. */
  372. Distance(value1: DeepImmutable<T>, value2: DeepImmutable<T>): number;
  373. /**
  374. * Returns the squared distance between the instances "value1" and "value2"
  375. * @param value1 defines first instance
  376. * @param value2 defines second instance
  377. * @returns the squared distance between instances
  378. */
  379. DistanceSquared(value1: DeepImmutable<T>, value2: DeepImmutable<T>): number;
  380. /**
  381. * Gets a new instance located at the center of the instances "value1" and "value2"
  382. * @param value1 defines first instance
  383. * @param value2 defines second instance
  384. * @returns a new instance
  385. */
  386. Center(value1: DeepImmutable<T>, value2: DeepImmutable<T>): T;
  387. /**
  388. * Gets the center of the instances "value1" and "value2" and stores the result in the instance "ref"
  389. * @param value1 defines first instance
  390. * @param value2 defines second instance
  391. * @param ref defines third instance
  392. * @returns ref
  393. */
  394. CenterToRef(value1: DeepImmutable<T>, value2: DeepImmutable<T>, ref: T): T;
  395. /**
  396. * Returns a new instance set with same the coordinates than "value" ones if the instance "value" is in the square defined by "min" and "max".
  397. * If a coordinate of "value" is lower than "min" coordinates, the returned instance is given this "min" coordinate.
  398. * If a coordinate of "value" is greater than "max" coordinates, the returned instance is given this "max" coordinate
  399. * @param value defines the value to clamp
  400. * @param min defines the lower limit
  401. * @param max defines the upper limit
  402. * @returns a new instance
  403. */
  404. Clamp(value: DeepImmutable<T>, min: DeepImmutable<T>, max: DeepImmutable<T>): T;
  405. /**
  406. * Returns a new instance set with same the coordinates than "value" ones if the instance "value" is in the square defined by "min" and "max".
  407. * If a coordinate of "value" is lower than "min" coordinates, the returned instance is given this "min" coordinate.
  408. * If a coordinate of "value" is greater than "max" coordinates, the returned instance is given this "max" coordinate
  409. * @param value defines the value to clamp
  410. * @param min defines the lower limit
  411. * @param max defines the upper limit
  412. * @param result defines the instance where to store the result
  413. * @returns the updated result instance
  414. */
  415. ClampToRef(value: DeepImmutable<T>, min: DeepImmutable<T>, max: DeepImmutable<T>, result: T): T;
  416. }
  417. export {};