September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,2 @@
blsq ) {{1 1} {1 0}} 10 .*{mm}r[
{{89 55} {55 34}}

View file

@ -0,0 +1,59 @@
using System;
using System.Collections;
using System.Collections.Generic;
using static System.Linq.Enumerable;
public static class MatrixExponentation
{
public static double[,] Identity(int size) {
double[,] matrix = new double[size, size];
for (int i = 0; i < size; i++) matrix[i, i] = 1;
return matrix;
}
public static double[,] Multiply(this double[,] left, double[,] right) {
if (left.ColumnCount() != right.RowCount()) throw new ArgumentException();
double[,] m = new double[left.RowCount(), right.ColumnCount()];
foreach (var (row, column) in from r in Range(0, m.RowCount()) from c in Range(0, m.ColumnCount()) select (r, c)) {
m[row, column] = Range(0, m.RowCount()).Sum(i => left[row, i] * right[i, column]);
}
return m;
}
public static double[,] Pow(this double[,] matrix, int exp) {
if (matrix.RowCount() != matrix.ColumnCount()) throw new ArgumentException("Matrix must be square.");
double[,] accumulator = Identity(matrix.RowCount());
for (int i = 0; i < exp; i++) {
accumulator = accumulator.Multiply(matrix);
}
return accumulator;
}
private static int RowCount(this double[,] matrix) => matrix.GetLength(0);
private static int ColumnCount(this double[,] matrix) => matrix.GetLength(1);
private static void Print(this double[,] m) {
foreach (var row in Rows()) {
Console.WriteLine("[ " + string.Join(" ", row) + " ]");
}
Console.WriteLine();
IEnumerable<IEnumerable<double>> Rows() =>
Range(0, m.RowCount()).Select(row => Range(0, m.ColumnCount()).Select(column => m[row, column]));
}
public static void Main() {
var matrix = new double[,] {
{ 3, 2 },
{ 2, 1 }
};
matrix.Pow(0).Print();
matrix.Pow(1).Print();
matrix.Pow(2).Print();
matrix.Pow(3).Print();
matrix.Pow(4).Print();
matrix.Pow(50).Print();
}
}

View file

@ -0,0 +1,71 @@
package main
import "fmt"
type vector = []float64
type matrix []vector
func (m1 matrix) mul(m2 matrix) matrix {
rows1, cols1 := len(m1), len(m1[0])
rows2, cols2 := len(m2), len(m2[0])
if cols1 != rows2 {
panic("Matrices cannot be multiplied.")
}
result := make(matrix, rows1)
for i := 0; i < rows1; i++ {
result[i] = make(vector, cols2)
for j := 0; j < cols2; j++ {
for k := 0; k < rows2; k++ {
result[i][j] += m1[i][k] * m2[k][j]
}
}
}
return result
}
func identityMatrix(n int) matrix {
if n < 1 {
panic("Size of identity matrix can't be less than 1")
}
ident := make(matrix, n)
for i := 0; i < n; i++ {
ident[i] = make(vector, n)
ident[i][i] = 1
}
return ident
}
func (m matrix) pow(n int) matrix {
le := len(m)
if le != len(m[0]) {
panic("Not a square matrix")
}
switch {
case n < 0:
panic("Negative exponents not supported")
case n == 0:
return identityMatrix(le)
case n == 1:
return m
}
pow := identityMatrix(le)
base := m
e := n
for e > 0 {
if (e & 1) == 1 {
pow = pow.mul(base)
}
e >>= 1
base = base.mul(base)
}
return pow
}
func main() {
m := matrix{{3, 2}, {2, 1}}
for i := 0; i <= 10; i++ {
fmt.Println("** Power of", i, "**")
fmt.Println(m.pow(i))
fmt.Println()
}
}

View file

@ -0,0 +1,3 @@
/Matrix Exponentiation
/mpow.k
pow: {:[0=y; :({a=/:a:!x}(#x))];a: x; do[y-1; a: x _mul a]; :a}

View file

@ -0,0 +1,32 @@
class Mat(list) :
def __matmul__(self, B) :
A = self
return Mat([[sum(A[i][k]*B[k][j] for k in range(len(B)))
for j in range(len(B[0])) ] for i in range(len(A))])
def identity(size):
size = range(size)
return [[(i==j)*1 for i in size] for j in size]
def power(F, n):
result = Mat(identity(len(F)))
b = Mat(F)
while n > 0:
if (n%2) == 0:
b = b @ b
n //= 2
else:
result = b @ result
b = b @ b
n //= 2
return result
def printtable(data):
for row in data:
print (' '.join('%-5s' % ('%s' % cell) for cell in row))
m = [[3,2], [2,1]]
for i in range(5):
print('\n%i:' % i)
printtable(power(m, i))

View file

@ -0,0 +1,56 @@
Option Base 1
Private Function Identity(n As Integer) As Variant
Dim I() As Variant
ReDim I(n, n)
For j = 1 To n
For k = 1 To n
I(j, k) = 0
Next k
Next j
For j = 1 To n
I(j, j) = 1
Next j
Identity = I
End Function
Function MatrixExponentiation(ByVal x As Variant, ByVal n As Integer) As Variant
If n < 0 Then
x = WorksheetFunction.MInverse(x)
n = -n
End If
If n = 0 Then
MatrixExponentiation = Identity(UBound(x))
Exit Function
End If
Dim y() As Variant
y = Identity(UBound(x))
Do While n > 1
If n Mod 2 = 0 Then
x = WorksheetFunction.MMult(x, x)
n = n / 2
Else
y = WorksheetFunction.MMult(x, y)
x = WorksheetFunction.MMult(x, x)
n = (n - 1) / 2
End If
Loop
MatrixExponentiation = WorksheetFunction.MMult(x, y)
End Function
Public Sub pp(x As Variant)
For i_ = 1 To UBound(x)
For j_ = 1 To UBound(x)
Debug.Print x(i_, j_),
Next j_
Debug.Print
Next i_
End Sub
Public Sub main()
M2 = [{3,2;2,1}]
M3 = [{1,2,0;0,3,1;1,0,0}]
pp MatrixExponentiation(M2, -1)
Debug.Print
pp MatrixExponentiation(M2, 0)
Debug.Print
pp MatrixExponentiation(M2, 10)
Debug.Print
pp MatrixExponentiation(M3, 10)
End Sub