Skip to content

Commit dc55835

Browse files
committed
docs(zynml): add basic_tensor.zynml example demonstrating operator overloading
Shows tensor arithmetic with +, -, * operators that dispatch to $Tensor$add, $Tensor$sub, $Tensor$mul trait methods.
1 parent 34a57d1 commit dc55835

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ZynML Example: Basic Tensor Operations
2+
// Run with: zynml run examples/basic_tensor.zynml
3+
4+
fn main() {
5+
// Create tensors
6+
let a = tensor([1.0, 2.0, 3.0])
7+
let b = tensor([4.0, 5.0, 6.0])
8+
9+
// Test addition with + operator (trait dispatch to $Tensor$add)
10+
let c = a + b
11+
extern tensor_println(c) // Should print: [5.0, 7.0, 9.0]
12+
13+
// Test multiplication with * operator (trait dispatch to $Tensor$mul)
14+
let d = a * b
15+
extern tensor_println(d) // Should print: [4.0, 10.0, 18.0]
16+
17+
// Test subtraction
18+
let e = b - a
19+
extern tensor_println(e) // Should print: [3.0, 3.0, 3.0]
20+
}

0 commit comments

Comments
 (0)