init: first working impl of determinant
types: added initial matrix definition and impl blocks
This commit is contained in:
commit
8bb435ab10
10 changed files with 279 additions and 0 deletions
3
.envrc
Normal file
3
.envrc
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
source_url "https://raw.githubusercontent.com/cachix/devenv/82c0147677e510b247d8b9165c54f73d32dfd899/direnvrc" "sha256-7u4iDd1nZpxL4tCzmPG0dQgC5V+/44Ba+tHkPob1v2k="
|
||||||
|
|
||||||
|
use devenv
|
||||||
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
# Devenv
|
||||||
|
.devenv*
|
||||||
|
devenv.local.nix
|
||||||
|
|
||||||
|
# direnv
|
||||||
|
.direnv
|
||||||
|
|
||||||
|
# pre-commit
|
||||||
|
.pre-commit-config.yaml
|
||||||
|
|
||||||
|
|
||||||
|
# Added by cargo
|
||||||
|
|
||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "matrix-rs"
|
||||||
|
version = "0.1.0"
|
||||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "matrix-rs"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
100
devenv.lock
Normal file
100
devenv.lock
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"devenv": {
|
||||||
|
"locked": {
|
||||||
|
"dir": "src/modules",
|
||||||
|
"lastModified": 1737321981,
|
||||||
|
"owner": "cachix",
|
||||||
|
"repo": "devenv",
|
||||||
|
"rev": "a30b871f21bfc987b65356909d04186a18adf0c6",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"dir": "src/modules",
|
||||||
|
"owner": "cachix",
|
||||||
|
"repo": "devenv",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"flake-compat": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1733328505,
|
||||||
|
"owner": "edolstra",
|
||||||
|
"repo": "flake-compat",
|
||||||
|
"rev": "ff81ac966bb2cae68946d5ed5fc4994f96d0ffec",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "edolstra",
|
||||||
|
"repo": "flake-compat",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"gitignore": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": [
|
||||||
|
"pre-commit-hooks",
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1709087332,
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "gitignore.nix",
|
||||||
|
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "hercules-ci",
|
||||||
|
"repo": "gitignore.nix",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1733477122,
|
||||||
|
"owner": "cachix",
|
||||||
|
"repo": "devenv-nixpkgs",
|
||||||
|
"rev": "7bd9e84d0452f6d2e63b6e6da29fe73fac951857",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "cachix",
|
||||||
|
"ref": "rolling",
|
||||||
|
"repo": "devenv-nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"pre-commit-hooks": {
|
||||||
|
"inputs": {
|
||||||
|
"flake-compat": "flake-compat",
|
||||||
|
"gitignore": "gitignore",
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1737465171,
|
||||||
|
"owner": "cachix",
|
||||||
|
"repo": "pre-commit-hooks.nix",
|
||||||
|
"rev": "9364dc02281ce2d37a1f55b6e51f7c0f65a75f17",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "cachix",
|
||||||
|
"repo": "pre-commit-hooks.nix",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"devenv": "devenv",
|
||||||
|
"nixpkgs": "nixpkgs",
|
||||||
|
"pre-commit-hooks": "pre-commit-hooks"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
49
devenv.nix
Normal file
49
devenv.nix
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
{ pkgs, lib, config, inputs, ... }:
|
||||||
|
|
||||||
|
{
|
||||||
|
# https://devenv.sh/basics/
|
||||||
|
env.GREET = "devenv";
|
||||||
|
|
||||||
|
# https://devenv.sh/packages/
|
||||||
|
packages = [ pkgs.git ];
|
||||||
|
|
||||||
|
# https://devenv.sh/languages/
|
||||||
|
languages.rust.enable = true;
|
||||||
|
|
||||||
|
# https://devenv.sh/processes/
|
||||||
|
processes.cargo-watch.exec = "cargo-watch";
|
||||||
|
|
||||||
|
# https://devenv.sh/services/
|
||||||
|
# services.postgres.enable = true;
|
||||||
|
|
||||||
|
# https://devenv.sh/scripts/
|
||||||
|
scripts.hello.exec = ''
|
||||||
|
echo hello from $GREET
|
||||||
|
'';
|
||||||
|
|
||||||
|
enterShell = ''
|
||||||
|
hello
|
||||||
|
git --version
|
||||||
|
'';
|
||||||
|
|
||||||
|
# https://devenv.sh/tasks/
|
||||||
|
# tasks = {
|
||||||
|
# "myproj:setup".exec = "mytool build";
|
||||||
|
# "devenv:enterShell".after = [ "myproj:setup" ];
|
||||||
|
# };
|
||||||
|
|
||||||
|
# https://devenv.sh/tests/
|
||||||
|
enterTest = ''
|
||||||
|
echo "Running tests"
|
||||||
|
git --version | grep --color=auto "${pkgs.git.version}"
|
||||||
|
'';
|
||||||
|
|
||||||
|
# https://devenv.sh/pre-commit-hooks/
|
||||||
|
pre-commit.hooks = {
|
||||||
|
clippy.enable = true;
|
||||||
|
commitizen.enable = true;
|
||||||
|
shellcheck.enable = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
# See full reference at https://devenv.sh/reference/options/
|
||||||
|
}
|
||||||
15
devenv.yaml
Normal file
15
devenv.yaml
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
# yaml-language-server: $schema=https://devenv.sh/devenv.schema.json
|
||||||
|
inputs:
|
||||||
|
nixpkgs:
|
||||||
|
url: github:cachix/devenv-nixpkgs/rolling
|
||||||
|
|
||||||
|
# If you're using non-OSS software, you can set allowUnfree to true.
|
||||||
|
# allowUnfree: true
|
||||||
|
|
||||||
|
# If you're willing to use a package that's vulnerable
|
||||||
|
# permittedInsecurePackages:
|
||||||
|
# - "openssl-1.1.1w"
|
||||||
|
|
||||||
|
# If you have more than one devenv you can merge them
|
||||||
|
#imports:
|
||||||
|
# - ./backend
|
||||||
9
src/main.rs
Normal file
9
src/main.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
use types::matrix::Matrix;
|
||||||
|
|
||||||
|
mod types;
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
let m = Matrix::from_str("1,2\n3,4".to_string());
|
||||||
|
let d = m.determinant();
|
||||||
|
println!("{}", d);
|
||||||
|
}
|
||||||
1
src/types.rs
Normal file
1
src/types.rs
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod matrix;
|
||||||
75
src/types/matrix.rs
Normal file
75
src/types/matrix.rs
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
/// Matrix
|
||||||
|
pub struct Matrix {
|
||||||
|
pub nrows: usize,
|
||||||
|
pub ncols: usize,
|
||||||
|
pub data: Vec<Vec<i32>>,
|
||||||
|
}
|
||||||
|
impl Matrix {
|
||||||
|
pub fn new(data: Vec<Vec<i32>>) -> Matrix {
|
||||||
|
Matrix {
|
||||||
|
nrows: data[0].len(),
|
||||||
|
ncols: data.len(),
|
||||||
|
data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn from_str(s: String) -> Matrix {
|
||||||
|
let mut d: Vec<Vec<i32>> = Vec::new();
|
||||||
|
let rows_iter = s.split('\n');
|
||||||
|
for (_i, txt) in rows_iter.enumerate() {
|
||||||
|
let mut r: Vec<i32> = Vec::new();
|
||||||
|
for (_j, ch) in txt.split(',').enumerate() {
|
||||||
|
let parsed = match i32::from_str(ch) {
|
||||||
|
Ok(n) => n,
|
||||||
|
Err(e) => panic!("Err: {}", e),
|
||||||
|
};
|
||||||
|
r.push(parsed);
|
||||||
|
}
|
||||||
|
d.push(r);
|
||||||
|
}
|
||||||
|
Matrix::new(d)
|
||||||
|
}
|
||||||
|
pub fn is_square(&self) -> bool {
|
||||||
|
&self.nrows == &self.ncols
|
||||||
|
}
|
||||||
|
pub fn splice(&self, at_index: usize) -> Matrix {
|
||||||
|
let mut data: Vec<Vec<i32>> = Vec::new();
|
||||||
|
for i in 0..self.data.len() {
|
||||||
|
if i == at_index {continue;}
|
||||||
|
let mut r: Vec<i32> = Vec::new();
|
||||||
|
for j in 0..self.data[i].len() {
|
||||||
|
if j == at_index {continue;}
|
||||||
|
r.push(self.data[i][j]);
|
||||||
|
}
|
||||||
|
data.push(r);
|
||||||
|
}
|
||||||
|
Matrix::new(data)
|
||||||
|
}
|
||||||
|
pub fn determinant(&self) -> i32 {
|
||||||
|
if self.nrows == 2 && self.nrows == 2 {
|
||||||
|
return &self.data[0][0] * &self.data[0][1] - &self.data[1][0] * &self.data[1][1];
|
||||||
|
}
|
||||||
|
let mut tmp = 0;
|
||||||
|
for (i, n) in self.data[0].iter().enumerate() {
|
||||||
|
let mult = if i % 2 == 0 { -*n } else { *n };
|
||||||
|
let eval = self.splice(i).determinant();
|
||||||
|
tmp += mult * eval;
|
||||||
|
}
|
||||||
|
tmp
|
||||||
|
}
|
||||||
|
pub fn transpose(&self) -> Matrix {
|
||||||
|
let mut new_data = Vec::<Vec<i32>>::new();
|
||||||
|
for i in 0..self.nrows {
|
||||||
|
let mut new_row = Vec::<i32>::new();
|
||||||
|
for j in 0..self.ncols {
|
||||||
|
new_row.push(self.data[j][i]);
|
||||||
|
}
|
||||||
|
new_data.push(new_row);
|
||||||
|
}
|
||||||
|
Matrix {
|
||||||
|
nrows: self.ncols,
|
||||||
|
ncols: self.nrows,
|
||||||
|
data: new_data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue