Skip to content

Commit

Permalink
Patches (#3)
Browse files Browse the repository at this point in the history
* no_std fixes and simplification of traits

* remove faulty test

* cleanup and improve memory usage
  • Loading branch information
ivajon committed Feb 1, 2024
1 parent 96872ca commit 112654b
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 636 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ version = "0.2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num = {version = "0.4.0",default-features = false}

num-traits = { version = "0.2", default-features = false, features = ["libm"] }
array-init = "2.1.0"
[features]
experimental = []
15 changes: 11 additions & 4 deletions examples/embedded/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use nrf52840_hal as _;
use panic_halt as _;
#[rtic::app(device = nrf52840_hal::pac)]
#[rtic::app(device = nrf52840_hal::pac, dispatchers = [TIMER0])]
mod app {
use matrs::predule::*;
use nrf52840_hal as hal;
Expand All @@ -25,18 +25,25 @@ mod app {
let m1: Matrix<u32, 2, 3> = [[1, 0, 10], [1, 0, 0]].into();
let m2: Matrix<u32, 3, 2> = [[2, 0], [1, 0], [1, 1]].into();
let res = m1.clone() * m2.clone();
foo::spawn().unwrap();
rprintln!("{:?}*{:?} = {:?}", m1, m2, res);

(Shared {}, Local {}, init::Monotonics())
}

#[task]
#[inline(never)]
fn foo(_: foo::Context) {
cortex_m::asm::nop();
}

#[idle]
fn idle(_cx: idle::Context) -> ! {
rprintln!("idle");
//rprintln!("idle");

panic!("panic");
//panic!("panic");

#[allow(unreachable_code)]
//#[allow(unreachable_code)]
loop {
continue;
}
Expand Down
34 changes: 21 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,43 @@ pub use crate::traits::{CompliantNumerical, MatrixInterface, VectorTrait};
use crate::vec::*;
use core::ops::Mul;
// Defines a method to transform a vector with a matrix
impl<T: CompliantNumerical, const ROWS: usize, const COLS: usize> Mul<Matrix<T, ROWS, COLS>>
for Vector<T, ROWS>
impl<
T: CompliantNumerical + Mul<TOther, Output = T>,
TOther: CompliantNumerical,
const ROWS: usize,
const COLS: usize,
> Mul<Matrix<TOther, ROWS, COLS>> for Vector<T, ROWS>
{
type Output = Vector<T, ROWS>;
/// Defines a method to transform a vector with a matrix
fn mul(self, other: Matrix<T, ROWS, COLS>) -> Vector<T, ROWS> {
fn mul(self, other: Matrix<TOther, ROWS, COLS>) -> Vector<T, ROWS> {
let mut result = Vector::new();
for row in 0..ROWS {
let mut sum: T = T::default();
for col in 0..COLS {
sum = sum + *self.get(row) * *other.get(row, col);
sum += self.get(row).clone() * other.get(row, col).clone();
}
result.set(row, sum);
}
result
}
}

impl<T: CompliantNumerical, const ROWS: usize, const COLS: usize> Mul<Vector<T, COLS>>
for Matrix<T, ROWS, COLS>
impl<
T: CompliantNumerical + Mul<TOther, Output = T>,
TOther: CompliantNumerical,
const ROWS: usize,
const COLS: usize,
> Mul<Vector<TOther, COLS>> for Matrix<T, ROWS, COLS>
{
type Output = Vector<T, ROWS>;
/// Defines a method to multiply a matrix with a vector
fn mul(self, other: Vector<T, COLS>) -> Vector<T, ROWS> {
fn mul(self, other: Vector<TOther, COLS>) -> Vector<T, ROWS> {
let mut result = Vector::new();
for row in 0..ROWS {
let mut sum: T = T::default();
for col in 0..COLS {
sum = sum + *self.get(row, col) * *other.get(col);
sum += self.get(row, col).clone() * other.get(col).clone();
}
result.set(row, sum);
}
Expand All @@ -108,7 +116,7 @@ impl<T: CompliantNumerical, const COUNT: usize> Vector<T, COUNT> {
pub fn to_matrix(self) -> Matrix<T, COUNT, 1> {
let mut result = Matrix::new();
for i in 0..COUNT {
result.set(i, 0, *self.get(i));
result.set(i, 0, self.get(i).clone());
}
result
}
Expand All @@ -119,7 +127,7 @@ impl<T: CompliantNumerical, const ROWS: usize> Matrix<T, ROWS, 1> {
pub fn row_vec(self) -> Vector<T, ROWS> {
let mut result = Vector::new();
for i in 0..ROWS {
result.set(i, *self.get(i, 0));
result.set(i, self.get(i, 0).clone());
}
result
}
Expand All @@ -130,7 +138,7 @@ impl<T: CompliantNumerical, const COLS: usize> Matrix<T, 1, COLS> {
pub fn col_vec(self) -> Vector<T, COLS> {
let mut result = Vector::new();
for i in 0..COLS {
result.set(i, *self.get(0, i));
result.set(i, self.get(0, i).clone());
}
result
}
Expand All @@ -139,10 +147,10 @@ impl<T: CompliantNumerical, const ROWS: usize, const COLS: usize> Matrix<T, ROWS
/// Creates an array of vectors from a matrix

pub fn to_array_of_vecs(self) -> [Vector<T, COLS>; ROWS] {
let mut result: [Vector<T, COLS>; ROWS] = [Vector::new(); ROWS];
let mut result: [Vector<T, COLS>; ROWS] = array_init::array_init(|_| Vector::new());
let data = self.get_elements();
for i in 0..ROWS {
result[i] = Vector::new_from_data(data[i]);
result[i] = Vector::new_from_data(data[i].clone());
}
result
}
Expand Down
Loading

0 comments on commit 112654b

Please sign in to comment.