summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhongheng Liu <z.liu@outlook.com.gr>2025-01-22 12:32:22 +0200
committerZhongheng Liu <z.liu@outlook.com.gr>2025-01-22 12:32:22 +0200
commit8bb435ab105e3f7f1374059dfd9365132167646d (patch)
tree07903b7b15e5a02308b6cfed063e38da6a3b9b1a
downloadmatrix-rs-8bb435ab105e3f7f1374059dfd9365132167646d.tar.gz
matrix-rs-8bb435ab105e3f7f1374059dfd9365132167646d.tar.bz2
matrix-rs-8bb435ab105e3f7f1374059dfd9365132167646d.zip
init: first working impl of determinant
types: added initial matrix definition and impl blocks
-rw-r--r--.envrc3
-rw-r--r--.gitignore14
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml6
-rw-r--r--devenv.lock100
-rw-r--r--devenv.nix49
-rw-r--r--devenv.yaml15
-rw-r--r--src/main.rs9
-rw-r--r--src/types.rs1
-rw-r--r--src/types/matrix.rs75
10 files changed, 279 insertions, 0 deletions
diff --git a/.envrc b/.envrc
new file mode 100644
index 0000000..894571b
--- /dev/null
+++ b/.envrc
@@ -0,0 +1,3 @@
+source_url "https://raw.githubusercontent.com/cachix/devenv/82c0147677e510b247d8b9165c54f73d32dfd899/direnvrc" "sha256-7u4iDd1nZpxL4tCzmPG0dQgC5V+/44Ba+tHkPob1v2k="
+
+use devenv
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b972a7f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,14 @@
+# Devenv
+.devenv*
+devenv.local.nix
+
+# direnv
+.direnv
+
+# pre-commit
+.pre-commit-config.yaml
+
+
+# Added by cargo
+
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..77576c2
--- /dev/null
+++ b/Cargo.lock
@@ -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"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..48996ca
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "matrix-rs"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/devenv.lock b/devenv.lock
new file mode 100644
index 0000000..989bc39
--- /dev/null
+++ b/devenv.lock
@@ -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
+}
diff --git a/devenv.nix b/devenv.nix
new file mode 100644
index 0000000..e1ce518
--- /dev/null
+++ b/devenv.nix
@@ -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/
+}
diff --git a/devenv.yaml b/devenv.yaml
new file mode 100644
index 0000000..116a2ad
--- /dev/null
+++ b/devenv.yaml
@@ -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
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..0e25ddc
--- /dev/null
+++ b/src/main.rs
@@ -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);
+}
diff --git a/src/types.rs b/src/types.rs
new file mode 100644
index 0000000..3bb3506
--- /dev/null
+++ b/src/types.rs
@@ -0,0 +1 @@
+pub mod matrix;
diff --git a/src/types/matrix.rs b/src/types/matrix.rs
new file mode 100644
index 0000000..ff292fc
--- /dev/null
+++ b/src/types/matrix.rs
@@ -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,
+ }
+ }
+}