June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public static class ElementWiseOperations
|
||||
{
|
||||
private static readonly Dictionary<string, Func<double, double, double>> operations =
|
||||
new Dictionary<string, Func<double, double, double>> {
|
||||
{ "add", (a, b) => a + b },
|
||||
{ "sub", (a, b) => a - b },
|
||||
{ "mul", (a, b) => a * b },
|
||||
{ "div", (a, b) => a / b },
|
||||
{ "pow", (a, b) => Math.Pow(a, b) }
|
||||
};
|
||||
|
||||
private static readonly Func<double, double, double> nothing = (a, b) => a;
|
||||
|
||||
public static double[,] DoOperation(this double[,] m, string name, double[,] other) =>
|
||||
DoOperation(m, operations.TryGetValue(name, out var operation) ? operation : nothing, other);
|
||||
|
||||
public static double[,] DoOperation(this double[,] m, Func<double, double, double> operation, double[,] other) {
|
||||
if (m == null || other == null) throw new ArgumentNullException();
|
||||
int rows = m.GetLength(0), columns = m.GetLength(1);
|
||||
if (rows != other.GetLength(0) || columns != other.GetLength(1)) {
|
||||
throw new ArgumentException("Matrices have different dimensions.");
|
||||
}
|
||||
|
||||
double[,] result = new double[rows, columns];
|
||||
for (int r = 0; r < rows; r++) {
|
||||
for (int c = 0; c < columns; c++) {
|
||||
result[r, c] = operation(m[r, c], other[r, c]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static double[,] DoOperation(this double[,] m, string name, double number) =>
|
||||
DoOperation(m, operations.TryGetValue(name, out var operation) ? operation : nothing, number);
|
||||
|
||||
public static double[,] DoOperation(this double[,] m, Func<double, double, double> operation, double number) {
|
||||
if (m == null) throw new ArgumentNullException();
|
||||
int rows = m.GetLength(0), columns = m.GetLength(1);
|
||||
double[,] result = new double[rows, columns];
|
||||
for (int r = 0; r < rows; r++) {
|
||||
for (int c = 0; c < columns; c++) {
|
||||
result[r, c] = operation(m[r, c], number);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void Print(this double[,] m) {
|
||||
if (m == null) throw new ArgumentNullException();
|
||||
int rows = m.GetLength(0), columns = m.GetLength(1);
|
||||
for (int r = 0; r < rows; r++) {
|
||||
Console.WriteLine("[ " + string.Join(", ", Enumerable.Range(0, columns).Select(c => m[r, c])) + " ]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main() {
|
||||
double[,] matrix = {
|
||||
{ 1, 2, 3, 4 },
|
||||
{ 5, 6, 7, 8 },
|
||||
{ 9, 10, 11, 12 }
|
||||
};
|
||||
|
||||
double[,] tens = {
|
||||
{ 10, 10, 10, 10 },
|
||||
{ 20, 20, 20, 20 },
|
||||
{ 30, 30, 30, 30 }
|
||||
};
|
||||
|
||||
matrix.Print();
|
||||
WriteLine();
|
||||
|
||||
(matrix = matrix.DoOperation("add", tens)).Print();
|
||||
WriteLine();
|
||||
|
||||
matrix.DoOperation((a, b) => b - a, 100).Print();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
program element_operations
|
||||
implicit none
|
||||
|
||||
real(kind=4), dimension(3,3) :: a,b
|
||||
integer :: i
|
||||
|
||||
a=reshape([(i,i=1,9)],shape(a))
|
||||
|
||||
print*,'addition'
|
||||
b=a+a
|
||||
call print_arr(b)
|
||||
|
||||
print*,'multiplication'
|
||||
b=a*a
|
||||
call print_arr(b)
|
||||
|
||||
print*,'division'
|
||||
b=a/b
|
||||
call print_arr(b)
|
||||
|
||||
print*,'exponentiation'
|
||||
b=a**a
|
||||
call print_arr(b)
|
||||
|
||||
print*,'trignometric'
|
||||
b=cos(a)
|
||||
call print_arr(b)
|
||||
|
||||
print*,'mod'
|
||||
b=mod(int(a),3)
|
||||
call print_arr(b)
|
||||
|
||||
print*,'element selection'
|
||||
b=0
|
||||
where(a>3) b=1
|
||||
call print_arr(b)
|
||||
|
||||
print*,'elemental functions can be applied to single values:'
|
||||
print*,square(3.0)
|
||||
print*,'or element wise to arrays:'
|
||||
b=square(a)
|
||||
call print_arr(b)
|
||||
|
||||
|
||||
contains
|
||||
|
||||
elemental real function square(a)
|
||||
real, intent(in) :: a
|
||||
square=a*a
|
||||
end function square
|
||||
|
||||
subroutine print_arr(arr)
|
||||
real, intent(in) :: arr(:,:)
|
||||
integer :: i
|
||||
do i=1,size(arr,dim=2)
|
||||
print*,arr(:,i)
|
||||
end do
|
||||
end subroutine print_arr
|
||||
|
||||
|
||||
end program element_operations
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
// version 1.1.51
|
||||
|
||||
typealias Matrix = Array<DoubleArray>
|
||||
typealias Op = Double.(Double) -> Double
|
||||
|
||||
fun Double.dPow(exp: Double) = Math.pow(this, exp)
|
||||
|
||||
fun Matrix.elementwiseOp(other: Matrix, op: Op): Matrix {
|
||||
require(this.size == other.size && this[0].size == other[0].size)
|
||||
val result = Array(this.size) { DoubleArray(this[0].size) }
|
||||
for (i in 0 until this.size) {
|
||||
for (j in 0 until this[0].size) result[i][j] = this[i][j].op(other[i][j])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun Matrix.elementwiseOp(d: Double, op: Op): Matrix {
|
||||
val result = Array(this.size) { DoubleArray(this[0].size) }
|
||||
for (i in 0 until this.size) {
|
||||
for (j in 0 until this[0].size) result[i][j] = this[i][j].op(d)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun Matrix.print(name: Char?, scalar: Boolean? = false) {
|
||||
println(when (scalar) {
|
||||
true -> "m $name s"
|
||||
false -> "m $name m"
|
||||
else -> "m"
|
||||
} + ":")
|
||||
for (i in 0 until this.size) println(this[i].asList())
|
||||
println()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val ops = listOf(Double::plus, Double::minus, Double::times, Double::div, Double::dPow)
|
||||
val names = "+-*/^"
|
||||
val m = arrayOf(
|
||||
doubleArrayOf(3.0, 5.0, 7.0),
|
||||
doubleArrayOf(1.0, 2.0, 3.0),
|
||||
doubleArrayOf(2.0, 4.0, 6.0)
|
||||
)
|
||||
m.print(null, null)
|
||||
for ((i, op) in ops.withIndex()) m.elementwiseOp(m, op).print(names[i])
|
||||
val s = 2.0
|
||||
println("s = $s:\n")
|
||||
for ((i, op) in ops.withIndex()) m.elementwiseOp(s, op).print(names[i], true)
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
# Built-in element-wise operator ~
|
||||
|
||||
#addition
|
||||
<1,2,3;4,5,6> +~ 2;
|
||||
|
||||
#subtraction
|
||||
<2,3,1,4;0,-2,-2,1> -~ 4;
|
||||
|
||||
#multiplication
|
||||
<2,3,1,4;0,-2,-2,1> *~ 4;
|
||||
|
||||
#division
|
||||
<2,3,7,9;6,8,4,5;7,0,10,11> /~ 2;
|
||||
|
||||
#exponentiation
|
||||
<1,2,0; 7,2,7; 6,11,3>^~5;
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
my @a =
|
||||
[1,2,3],
|
||||
[4,5,6],
|
||||
[7,8,9];
|
||||
|
||||
sub msay(@x) {
|
||||
for @x -> @row {
|
||||
print ' ', $_%1 ?? $_.nude.join('/') !! $_ for @row;
|
||||
say '';
|
||||
}
|
||||
say '';
|
||||
}
|
||||
|
||||
msay @a «+» @a;
|
||||
msay @a «-» @a;
|
||||
msay @a «*» @a;
|
||||
msay @a «/» @a;
|
||||
msay @a «+» [1,2,3];
|
||||
msay @a «-» [1,2,3];
|
||||
msay @a «*» [1,2,3];
|
||||
msay @a «/» [1,2,3];
|
||||
msay @a «+» 2;
|
||||
msay @a «-» 2;
|
||||
msay @a «*» 2;
|
||||
msay @a «/» 2;
|
||||
|
||||
# In addition to calling the underlying higher-order functions directly, it's possible to name a function.
|
||||
|
||||
sub infix:<M+> (\l,\r) { l <<+>> r }
|
||||
|
||||
msay @a M+ @a;
|
||||
msay @a M+ [1,2,3];
|
||||
msay @a M+ 2;
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
var m1 = [[3,1,4],[1,5,9]]
|
||||
var m2 = [[2,7,1],[8,2,2]]
|
||||
|
||||
say ":: Matrix-matrix operations"
|
||||
say (m1 ~W+ m2)
|
||||
say (m1 ~W- m2)
|
||||
say (m1 ~W* m2)
|
||||
say (m1 ~W/ m2)
|
||||
say (m1 ~W// m2)
|
||||
say (m1 ~W** m2)
|
||||
say (m1 ~W% m2)
|
||||
|
||||
say "\n:: Matrix-scalar operations"
|
||||
say (m1 ~S+ 42)
|
||||
say (m1 ~S- 42)
|
||||
say (m1 ~S/ 42)
|
||||
say (m1 ~S** 10)
|
||||
# ...
|
||||
|
||||
say "\n:: Scalar-matrix operations"
|
||||
say (m1 ~RS+ 42)
|
||||
say (m1 ~RS- 42)
|
||||
say (m1 ~RS/ 42)
|
||||
say (m1 ~RS** 10)
|
||||
# ...
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
mata
|
||||
a = rnormal(5,5,0,1)
|
||||
b = 2
|
||||
a:+b
|
||||
a:-b
|
||||
a:*b
|
||||
a:/b
|
||||
a:^b
|
||||
|
||||
a = rnormal(5,5,0,1)
|
||||
b = rnormal(5,1,0,1)
|
||||
a:+b
|
||||
a:-b
|
||||
a:*b
|
||||
a:/b
|
||||
a:^b
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue