Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
trait Sub, det and almost finished gauss (#185)
Browse files Browse the repository at this point in the history
* Add rows

* Multiply when adding

* Add Sub trait

* Calculate det and almost solve gauss
  • Loading branch information
gavrilikhin-d authored May 19, 2024
1 parent 41ef509 commit 0136174
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 67 additions & 6 deletions benchmarks/linalg/linalg/src/main.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,82 @@ fn <rows: Integer> x <columns: Integer> Hilbert Matrix -> Matrix:
i += 1
return m

fn add <k: Rational> <a: RowSlice> to <b: MutRowSlice>:
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 <k: Rational> <a: MutRowSlice> to <b: MutRowSlice>:
add k (RowSlice from a) to b

fn add <a: RowSlice> to <b: MutRowSlice>:
add 1.0 a to b

fn add <a: MutRowSlice> to <b: MutRowSlice>:
add (RowSlice from a) to b

fn gauss <a: Matrix> <b: Matrix> -> Matrix:
fn det <a_: Matrix> -> 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 <a_: Matrix> <b_: Matrix> -> 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
11 changes: 11 additions & 0 deletions ppl/src/math.ppl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ trait Add:
self = self + other
//=================================

//=================================
// Add
//=================================
/// Trait for `-` operation
trait Sub:
fn <:Self> - <:Self> -> Self

fn <self: ReferenceMut<Self>> -= <other: Self>:
self = self - other
//=================================


//=================================
// Multiply
Expand Down

0 comments on commit 0136174

Please sign in to comment.