dimforge / dimforge/nalgebra

Calculations on Z/2Z?

Open
#723 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

Can I use nalgebra with with elements from Z/2Z (0 and 1)?

More specifically, I'm trying to solve linear equations in Z/2Z, e.g.

```
┌ ┐
│ 1 1 0 0 │
│ 1 1 0 1 │
A=│ 0 1 1 1 │ Solve Ax=0
│ 0 0 1 0 │
│ 0 0 0 1 │
└ ┘
```

There are solutions in Z/2Z (for example: `[1 1 0 0 1]t`), and apparently no solutions in R (see [this playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3b31434dbdf5d26537f62bbf336c513c))

#### Trying with `bool`

I read on [simba docs](https://docs.rs/simba/0.1.2/simba/) that

> Simba is a crate defining a set of trait for writing code that can be generic with regard to the number of lanes of the numeric input value. Those traits are implemented by f32, u32, i16, _**bool**_ as well as SIMD types like f32x4, u32x8, i16x2, etc.

But when I try to create a `Matrix`, I get the trait bound not satisfied:
```
no method named `full_piv_lu` found for struct `nalgebra::base::matrix::Matrix>` in the current scope
the method `lu` exists but the following trait bounds were not satisfied:
`bool : alga::general::complex::ComplexField`
```

Which makes sense, even `Add` and `Mul` are not implemented on `bool`.

#### Trying with my own type `Bool`

So then I tried creating my own wrapper:

```rust
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Bool(bool);

impl Bool {
fn to_bool(&self) -> &bool {
&self.0
}
}

impl Add for Bool {
type Output = Self;

fn add(self, rhs: Self) -> Self {
Bool(self.0 ^ rhs.0)
}
}

impl Mul for Bool {
type Output = Self;

fn mul(self, rhs: Self) -> Self {
Bool(self.0 & rhs.0)
}
}
```

Would I need to `impl ComplexField for Bool`? I'm asking here first, would like to know if that's the way to go, if it's at all possible, or if there are any other shortcuts?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.