Skip to content

This library can calculate matrix. Support lazy evaluation. The matrix obj can be loaded from/saved to plain text.

Notifications You must be signed in to change notification settings

BearOffice/Matrix-Library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Matrix-Library

This library can calculate matrix.
Support lazy evaluation.
The matrix obj can be loaded from/saved to plain text.

Usage (part)

Create a (2 * 3) matrix from 2D array.

var matrix1 = new Matrix<string>(new[,]
{
    { "12g", "36a", "54c" },
    { "98f", "32d", "75e" }
});
Console.WriteLine(matrix1 + "\n");

[["12g", "36a", "54c"]
 ["98f", "32d", "75e"]]


Queries(extended methods) from class MathQ support lazy evaluation.
.AsParallel() will let the remaining methods executed parallelly if needed.
.AsParallel(force: true) will force the remaining methods executed parallely.

var matrixseq = matrix1.AsParallel().Transpose().Map(str => int.Parse(str[0..^1]));
matrix1[0, 0] = "21b";

// Call .ToMatrix() will evaluate the sequence immediately.
var intmatrix1 = matrixseq.ToIntMatrix();
Console.WriteLine(intmatrix1 + "\n");

[[21, 98]
 [36, 32]
 [54, 75]]


Create a (3 * 2) blank int matrix and fill random numbers into it.

var intmatrix2 = new IntMatrix(3, 2);
var rand = new Random();
intmatrix2 = intmatrix2.Set((_, _) => rand.Next(10, 99)).ToIntMatrix();
Console.WriteLine(intmatrix2 + "\n");

[[74, 93]
 [74, 22]
 [76, 82]]


Concat the right matrix to the left matrix's right.

var intmatrix3 = intmatrix1 & intmatrix2;
Console.WriteLine(intmatrix3 + "\n");

[[21, 98, 74, 93]
 [36, 32, 74, 22]
 [54, 75, 76, 82]]


Create a (1 * 4) matrix from 1D array.

var intmatrix4 = new IntMatrix(new[] { 88, 77, 66, 55 });
Console.WriteLine(intmatrix4 + "\n");

[[88, 77, 66, 55]]


Concat the right matrix to the left matrix's bottom.

var intmatrix5 = intmatrix3 | intmatrix4;
Console.WriteLine(intmatrix5 + "\n");

[[21, 98, 74, 93]
 [36, 32, 74, 22]
 [54, 75, 76, 82]
 [88, 77, 66, 55]]


Create a matrix from string.

var matrixstr = "[[\"sfe\", \"wrf\"]\n [\"rhj\", \"sgd\"]\n [\"dfg\", \"qac\"]]";
Console.WriteLine(matrixstr + "\n");

var matrix2 = Matrix.GetMatrixFromString<string>(matrixstr, new ParseFromString());
Console.WriteLine(matrix2 + "\n");

[["sfe", "wrf"]
 ["rhj", "sgd"]
 ["dfg", "qac"]]

[["sfe", "wrf"]
 ["rhj", "sgd"]
 ["dfg", "qac"]]


Modify the matrix's parse rule.

var zippedmatrix = intmatrix1.Zip(matrix2).ToMatrix();

var rule = new ParseToString();
rule.Add(typeof((int, string)), item =>
{
    var tuple = ((int, string))item;
    return $"<{tuple.Item1}-{tuple.Item2}>";
});
Console.WriteLine(zippedmatrix.ToString(rule));

[["<21-sfe>", "<98-wrf>"]
 ["<36-rhj>", "<32-sgd>"]
 ["<54-dfg>", "<75-qac>"]]

About

This library can calculate matrix. Support lazy evaluation. The matrix obj can be loaded from/saved to plain text.

Topics

Resources

Stars

Watchers

Forks

Languages