{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Peroxide Guide" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook used [evcxr-jupyter](https://github.com/google/evcxr/blob/master/evcxr_jupyter/README.md)\n", "\n", "These examples correspond to `peroxide = 0.6.4`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "extern crate peroxide;\n", "use peroxide::*;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Module Structure" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```bash\n", "├── macros\n", "│ ├── matlab_macro.rs\n", "│ ├── mod.rs\n", "│ └── r_macro.rs\n", "├── ml\n", "│ ├── mod.rs\n", "│ └── reg.rs\n", "├── numerical\n", "│ ├── interp.rs\n", "│ ├── mod.rs\n", "│ └── spline.rs\n", "├── operation\n", "│ ├── extra_ops.rs\n", "│ └── mod.rs\n", "├── statistics\n", "│ ├── mod.rs\n", "│ ├── rand.rs\n", "│ └── stat.rs\n", "├── structure\n", "│ ├── dual.rs\n", "│ ├── matrix.rs\n", "│ ├── mod.rs\n", "│ ├── polynomial.rs\n", "│ └── vector.rs\n", "└── util\n", " ├── mod.rs\n", " ├── non_macro.rs\n", " └── print.rs\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Vector (`Vec`)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1) Declare Vector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can declare vector rust-way" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n" ] } ], "source": [ "let a: Vec = vec![1.,2.,3.,4.];\n", "a.print(); // Convenient to print" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And also R-way" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n" ] } ], "source": [ "let b = c!(1,2,3,4); // R-like macro to declare Vec\n", "b.print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And you can use R's `seq` function" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 5, 7, 9]\n" ] } ], "source": [ "let c = seq!(1,10,2); // start, end, step\n", "c.print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2) Functional Programming Tool" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is original rust way to FP" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 4, 5]\n" ] } ], "source": [ "a.clone().into_iter().map(|x| x + 1.).collect::().print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is `Peroxide` way" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 4, 5]\n" ] } ], "source": [ "a.fmap(|x| x + 1.).print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use `fmap, reduce, zip_with` as follows" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "[2, 4, 6, 8]\n" ] } ], "source": [ "// reduce\n", "a.reduce(0, |x, y| x + y).print();\n", "// zip_with\n", "a.zip_with(|x, y| x + y, &b).print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3) Simple Vector operation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is VectorOps Trait\n", "\n", "```rust\n", "pub trait VecOps {\n", " type Scalar;\n", " fn add(&self, other: &Self) -> Self;\n", " fn sub(&self, other: &Self) -> Self;\n", " fn mul(&self, other: &Self) -> Self;\n", " fn div(&self, other: &Self) -> Self;\n", " fn dot(&self, other: &Self) -> Self::Scalar;\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4, 6, 8]\n", "[0, 0, 0, 0]\n", "[1, 4, 9, 16]\n", "[1, 1, 1, 1]\n", "30\n" ] } ], "source": [ "// Addition\n", "a.add(&b).print();\n", "a.sub(&b).print();\n", "a.mul(&b).print();\n", "a.div(&b).print();\n", "a.dot(&b).print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1) Declare Matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are various ways to declare matrix.\n", "Belows are all same" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "// Function way\n", "let m1 = matrix(vec![1,2,3,4], 2, 2, Row);\n", "let m2 = matrix(c!(1,2,3,4), 2, 2, Row);\n", "let m2_2 = matrix(c!(1,3,2,4), 2, 2, Col);\n", "let m3 = matrix(seq!(1, 4, 1), 2, 2, Row);\n", "\n", "// Macro way\n", "let m4 = matrix!(1;4;1, 2, 2, Row);" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " c[0] c[1]\n", " r[0] 1 2\n", " r[1] 3 4\n" ] } ], "source": [ "m4.print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peroxide allows smart print (But it's not applied to Jupyter)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " c[0] c[1]\n", " r[0] 2.0000 1\n", " r[1] 2 3\n" ] } ], "source": [ "let m0 = matrix(c!(1.99999, 1, 2, 3), 2, 2, Row);\n", "m0.print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2) Concatenate" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " c[0] c[1] c[2]\n", " r[0] 1 3 5\n", " r[1] 2 4 6\n" ] } ], "source": [ "let a = matrix!(1;4;1, 2, 2, Col);\n", "let b = matrix(c!(5, 6), 2, 1, Col);\n", "let c = cbind!(a, b);\n", "c.print();" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " c[0] c[1]\n", " r[0] 1 2\n", " r[1] 3 4\n", " r[2] 5 6\n" ] } ], "source": [ "let d = matrix!(1;4;1, 2, 2, Row);\n", "let e = matrix(c!(5, 6), 1, 2, Row);\n", "let f = rbind!(d, e);\n", "f.print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3) Matrix Operation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Caution: `std::ops` consume value. If you want to re-use the value, use `clone`" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Addition\n", " c[0] c[1]\n", " r[0] 2 5\n", " r[1] 5 8\n", "\n", "Subtraction\n", " c[0] c[1]\n", " r[0] 0 -1\n", " r[1] 1 0\n", "\n", "Component-wise multiplication\n", " c[0] c[1]\n", " r[0] 1 6\n", " r[1] 6 16\n", "\n", "Matrix multiplication\n", " c[0] c[1]\n", " r[0] 5 11\n", " r[1] 11 25\n" ] } ], "source": [ "let a = matrix!(1;4;1, 2, 2, Row);\n", "let b = matrix!(1;4;1, 2, 2, Col);\n", "\n", "println!(\"Addition\");\n", "(a.clone() + b.clone()).print();\n", "println!(\"\");\n", "\n", "println!(\"Subtraction\");\n", "(a.clone() - b.clone()).print();\n", "println!(\"\");\n", "\n", "println!(\"Component-wise multiplication\");\n", "(a.clone() * b.clone()).print(); // Component-wise multiplication\n", "println!(\"\");\n", "\n", "println!(\"Matrix multiplication\");\n", "(a.clone() % b.clone()).print(); // Matrix multiplication" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4) LU Decomposition" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Column Permutation: [(0, 1)]\n", "Row Permutation: [(0, 1)]\n", "Lower Triangular Matrix:\n", " c[0] c[1]\n", " r[0] 1 0\n", " r[1] 0.5 1\n", "Upper Triangular Matrix:\n", " c[0] c[1]\n", " r[0] 4 3\n", " r[1] 0 -0.5\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let a = matrix!(1;4;1, 2, 2, Row);\n", "let pqlu = a.lu(); // Option\n", "\n", "// Unwrap process\n", "match pqlu {\n", " None => println!(\"No LU Decomposition\"),\n", " Some(abcd) => {\n", " let (p, q, l, u) = (abcd.p, abcd.q, abcd.l, abcd.u);\n", " println!(\"Column Permutation: {:?}\", p);\n", " println!(\"Row Permutation: {:?}\", q);\n", " println!(\"Lower Triangular Matrix:\\n {}\", l);\n", " println!(\"Upper Triangular Matrix:\\n {}\", u);\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5) Determinant" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-2\n" ] } ], "source": [ "let a = matrix!(1;4;1, 2, 2, Row);\n", "a.det().print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6) Inverse" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " c[0] c[1]\n", " r[0] -2 1\n", " r[1] 1.5 -0.5\n", "\n", " c[0] c[1]\n", " r[0] -2 1\n", " r[1] 1.5 -0.5\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.inv().unwrap().print();\n", "\n", "// or\n", "println!(\"\");\n", "match a.inv() {\n", " None => println!(\"No inverse!\"),\n", " Some(m) => m.print(),\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7) Extract Column or Row" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3]\n", "[2, 4]\n", "[1, 2]\n", "[3, 4]\n" ] } ], "source": [ "let a = matrix!(1;4;1, 2, 2, Row);\n", "a.col(0).print();\n", "a.col(1).print();\n", "a.row(0).print();\n", "a.row(1).print();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8) Functional Programming" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " c[0] c[1]\n", " r[0] 2 3\n", " r[1] 4 5\n" ] } ], "source": [ "a.fmap(|x| x + 1f64).print();" ] } ], "metadata": { "kernelspec": { "display_name": "Rust", "language": "rust", "name": "rust" }, "language_info": { "codemirror_mode": "rust", "file_extension": ".rs", "mimetype": "text/rust", "name": "Rust", "pygment_lexer": "rust", "version": "" } }, "nbformat": 4, "nbformat_minor": 2 }