Skip to content

feat: algebra #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 3 additions & 255 deletions Cargo.lock

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

7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ readme = "README.md"
rust-version = "1.80.0"

[dependencies]
extensor-macros = { path = "macros/", version = "0.1.0" }


[dev-dependencies]
log = "0.4.21"
env_logger = "0.11.3"
const-default = "1.0"
39 changes: 39 additions & 0 deletions src/algebra/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use core::marker::PhantomData;

use module::{Module, Vector};
use tensor::Tensor;

use super::*;

pub mod quadratic_form;

// #[derive(Clone, Copy)]
// pub struct TensorAlgebra<M: Module>(PhantomData<M>);

// impl<M:Module> Module for TensorAlgebra<M> {
// type Ring = M::Ring;
// }

// impl<M:Module> Add for TensorAlgebra<M> {
// type Output = TensorAlgebra<M>;

// fn add(self, rhs: Self) -> Self::Output {
// todo!()
// }
// }

// impl<M:Module> Neg for TensorAlgebra<M> {
// type Output = TensorAlgebra<M>;

// fn neg(self) -> Self::Output {
// todo!()
// }
// }

// impl<M:Module> Mul<M::Ring> for TensorAlgebra<M> {
// type Output = TensorAlgebra<M>;

// fn mul(self, rhs: M::Ring) -> Self::Output {
// todo!()
// }
// }
63 changes: 63 additions & 0 deletions src/algebra/quadratic_form.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use core::ops::AddAssign;

use module::{Module, Vector};

use super::*;

pub struct SymmetricIndex<const D: usize>
where
[(); D * (D + 1) / 2]:,
{
// Store only upper triangular indices
// Length is (D * (D + 1)) / 2
indices: [(usize, usize); (D * (D + 1)) / 2],
}

pub struct QuadraticForm<const D: usize, F> {
eigenbasis: [Vector<D, F>; D],
eigenvalues: [F; D],
}

impl<const D: usize, F: Copy + ConstDefault + Mul<Output = F> + AddAssign> QuadraticForm<D, F> {
pub const fn new_diagonal(eigenvalues: [F; D]) -> Self {
let mut eigenbasis = [Vector::DEFAULT; D];
let mut i = 0;
while i < D {
let mut j = 0;
while j < D {
if i == j {
eigenbasis[i].0[i] = eigenvalues[i];
}
j += 1;
}
i += 1;
}

Self {
eigenbasis,
eigenvalues,
}
}

// TODO: Make `const` if we ever get a const `Mul`
pub fn eval(&self, lhs: Vector<D, F>, rhs: Vector<D, F>) -> F {
let mut sum = F::DEFAULT;
let mut i = 0;
while i < D {
// Project vectors onto eigenbasis
let mut lhs_comp = F::DEFAULT;
let mut rhs_comp = F::DEFAULT;
let mut j = 0;
while j < D {
lhs_comp += lhs.0[j] * self.eigenbasis[i].0[j];
rhs_comp += rhs.0[j] * self.eigenbasis[i].0[j];
j += 1;
}

// Multiply by eigenvalue and add to sum
sum += lhs_comp * rhs_comp * self.eigenvalues[i];
i += 1;
}
sum
}
}
Loading