diff --git a/README.md b/README.md index f5ebd60..0f7027b 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ ### Current task * [ ] Printable trait should take references --- +* [ ] mutable parameters * [ ] migrate to pass-by-ref (branch `arc`) * [ ] Prefer candidates with mutable references, when possible * [ ] Fix problems with to_ir and loading references (especially globals). This causes issues in iterator diff --git a/benchmarks/linalg/linalg/src/main.ppl b/benchmarks/linalg/linalg/src/main.ppl index f25109b..fa2fada 100644 --- a/benchmarks/linalg/linalg/src/main.ppl +++ b/benchmarks/linalg/linalg/src/main.ppl @@ -151,21 +151,82 @@ fn x Hilbert Matrix -> Matrix: i += 1 return m +fn add to : + assert a.matrix.columns == b.matrix.columns "Can't add rows of different lengths" + let n = a.matrix.columns + let mut i = 0 + while i < n: + b[i] += k * a[i] + i += 1 + +fn add to : + add k (RowSlice from a) to b + +fn add to : + add 1.0 a to b + +fn add to : + add (RowSlice from a) to b -fn gauss -> Matrix: +fn det -> Rational: + let mut a = a_ + assert a.rows == a.columns "Matrix must be square" + let n = a.rows + let mut i = 0 + while i < n: + let mut j = i + 1 + while j < n: + let k = (a[j])[i] / (a[i])[i] // FIXME: division by zero + if k != 0.0: + add -k (a[i]) to (a[j]) + j += 1 + i += 1 + + let mut d = 1.0 + i = 0 + while i < n: + d *= (a[i])[i] + i += 1 + return d + + +fn gauss -> Matrix: + let mut a = a_ + let mut b = b_ assert a.rows == a.columns "Matrix must be square" assert a.rows == b.rows "Matrix and vector must have the same number of rows" let n = a.rows + let mut i = 0 + while i < n: + let mut j = i + 1 + while j < n: + let k = (a[j])[i] / (a[i])[i] // FIXME: division by zero + if k != 0.0: + add -k (a[i]) to (a[j]) + (b[j])[0] -= k * (b[i])[0] + j += 1 + i += 1 + + i = n - 1 + while i > 0: + let mut j = i - 1 + while j >= 0: + let k = (a[j])[i] / (a[i])[i] // FIXME: division by zero + if k != 0.0: + add -k (a[i]) to (a[j]) + (b[j])[0] -= k * (b[i])[0] + j -= 1 + i -= 1 + return b // let m = 3 x 3 Hilbert Matrix // print m fn run code: - let mut a = 1 - let mut b = 2 - swap a and b - print a - print b + let mut a = 3 x 3 Hilbert Matrix + println (det a) +// let mut b = Vector from (repeat 1.0 3 times) +// gauss a b run code \ No newline at end of file diff --git a/ppl/src/math.ppl b/ppl/src/math.ppl index 622c2ca..5ebea97 100644 --- a/ppl/src/math.ppl +++ b/ppl/src/math.ppl @@ -11,6 +11,17 @@ trait Add: self = self + other //================================= +//================================= +// Add +//================================= +/// Trait for `-` operation +trait Sub: + fn <:Self> - <:Self> -> Self + + fn > -= : + self = self - other +//================================= + //================================= // Multiply