Skip to content

Latest commit

 

History

History
57 lines (47 loc) · 1.33 KB

README.md

File metadata and controls

57 lines (47 loc) · 1.33 KB

Operator Overloading In TypeScript

Using TS Compiler API to add operator overloading to TS

Examples

const a = new Tensor([
    [1, 2],
    [3, 4]
]);

const b = new Tensor([
    [5, 6],
    [7, 8]
]);

console.log('Tensor Tensor Multiplication Example');
// @ts-ignore
const c = a * b;
console.log("c: ", c);
console.log('-----------------------------------');

console.log('Tensor Scalar Multiplication Example (Scalar Right)');
// @ts-ignore
const d = a * 2;
console.log("d: ", d)
console.log('-----------------------------------');

console.log('Tensor Scalar Multiplication Example (Scalar Left)');
// @ts-ignore
const e = 2 * a;
console.log("e: ", e);
✔ ~/Code/ts/operator-overload.ts [main|✔] 
20:58 $ npm run example

> operator-overload-ts@1.0.0 example
> node dist/index.js

a:  Tensor { value: [ [ 1, 2 ], [ 3, 4 ] ] }
b:  Tensor { value: [ [ 5, 6 ], [ 7, 8 ] ] }
Tensor Tensor Multiplication Example
c:  Tensor { value: [ [ 19, 22 ], [ 43, 50 ] ] }
-----------------------------------
Tensor Scalar Multiplication Example (Scalar Right)
d:  Tensor { value: [ [ 2, 4 ], [ 6, 8 ] ] }
-----------------------------------
Tensor Scalar Multiplication Example (Scalar Left)
e:  Tensor { value: [ [ 2, 4 ], [ 6, 8 ] ] }

Compile

npm run build or npx ts-node src/compile.ts

Run Example

npm run example or node dist/index.js