Skip to content

Instantly share code, notes, and snippets.

@rik-degraaff
Last active October 29, 2017 18:10
Show Gist options
  • Save rik-degraaff/6911df24af080fc8ef53e6d48e32dffb to your computer and use it in GitHub Desktop.
Save rik-degraaff/6911df24af080fc8ef53e6d48e32dffb to your computer and use it in GitHub Desktop.
JMatrix stuff
package genericUtils;
public abstract class TypeParameter<T> {
abstract public Class<? extends T> getType();
abstract public <S extends T> void useType(Class<S> type);
public TypeParameter() {
useType(getType());
}
}
package integers;
public interface _N extends _N0 {
final static Object[] nums = new Object[]
{/*_0.class,*/ _1.class, _2.class, _3.class, _4.class, _5.class,
_6.class, _7.class, _8.class, _9.class, _10.class, _11.class};
public static int valueOf(Class<? extends _N> num) {
for (int n = 0; n < nums.length; n++) {
if (nums[n] == num) {
return n + 1;
}
}
throw new RuntimeException("this number does not exist.");
}
@SuppressWarnings("unchecked")
public static Class<? extends _N> fromValue(int value) {
return (Class<? extends _N>) nums[value - 1];
}
}
import integers.*;
public interface Matrix<N extends _N, M extends _N> {
public double getElementAt(int x, int y);
public void setElementAt(int x, int y, double value);
public int numRows();
public int numCols();
public default String asString() {
final String nl = System.getProperty("line.separator");
String str = "[" + nl;
for (int y = 0; y < numRows(); y++) {
str += " [ ";
for (int x = 0; x < numCols(); x++) {
str += getElementAt(x, y) + " ";
}
str += "]" + nl;
}
return str + "]";
}
@SuppressWarnings("unchecked")
public default Class<N> rows() {
return (Class<N>) _N.fromValue(numRows());
}
@SuppressWarnings("unchecked")
public default Class<M> cols() {
return (Class<M>) _N.fromValue(numCols());
}
public default Matrix<N, M> add(Matrix<N, M> other) {
Matrix<N, M> m = make(rows(), cols());
for (int y = 0; y < numRows(); y++) {
for (int x = 0; x < numCols(); x++) {
m.setElementAt(x, y, getElementAt(x, y) + other.getElementAt(x, y));
}
}
return m;
}
public default Matrix<N, M> multiply(double factor) {
Matrix<N, M> m = make(rows(), cols());
for (int y = 0; y < numRows(); y++) {
for (int x = 0; x < numCols(); x++) {
m.setElementAt(x, y, factor * getElementAt(x, y));
}
}
return m;
}
public default <P extends _N> Matrix<N, P> multiply(Matrix<M, P> other) {
Matrix<N, P> m = make(rows(), other.cols());
for (int y = 0; y < numRows(); y++) {
for (int x = 0; x < other.numCols(); x++) {
double sum = 0;
for (int i = 0; i < numCols(); i++) {
sum += getElementAt(i, y) * other.getElementAt(x, i);
}
m.setElementAt(x, y, sum);
}
}
return m;
}
public default <P extends _N, Q extends _N> Matrix<P, Q> crop(Class<P> r, Class<Q> c) {
return identity(r, rows()).multiply(this).multiply(identity(cols(), c));
}
public static <Rows extends _N, Cols extends _N> Matrix<Rows, Cols> make(Class<Rows> r, Class<Cols> c) {
return new ArrayMatrix<Rows, Cols>(r, c);
}
public static <Rows extends _N, Cols extends _N> Matrix<Rows, Cols> identity(Class<Rows> r, Class<Cols> c) {
Matrix<Rows, Cols> m = make(r, c);
for (int i = 0; i < Math.min(m.numRows(), m.numCols()); i++) {
m.setElementAt(i, i, 1);
}
return m;
}
public class ArrayMatrix<Rows extends _N, Cols extends _N> implements Matrix<Rows, Cols> {
private double[][] values;
private final int rows, cols;
ArrayMatrix(Class<Rows> r, Class<Cols> c) {
rows = _N.valueOf(r);
cols = _N.valueOf(c);
values = new double[rows][cols];
}
public double getElementAt(int x, int y) {
return values[y][x];
}
public void setElementAt(int x, int y, double value) {
values[y][x] = value;
}
public int numRows() {
return rows;
}
public int numCols() {
return cols;
}
}
}
import integers.*;
import genericUtils.TypeParameter;
public class MatrixTest {
public static void main(String[] args) {
Matrix<_2, _3> m23 = Matrix.make(_2.class, _3.class);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 2; y++) {
m23.setElementAt(x, y, x + 2*y);
}
}
System.out.println("0: " + m23.asString());
System.out.println("1: " + m23.crop(_1.class, _2.class).asString());
System.out.println("2: " + m23.crop(_5.class, _7.class).asString());
System.out.println("3: " + m23.multiply(2).asString());
Matrix<_3, _2> m32 = Matrix.make(_3.class, _2.class);
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 3; y++) {
m32.setElementAt(x, y, 2 + x);
}
}
System.out.println("4: " + m23.multiply(m32).asString());
//Matrix.make(_3.class, _5.class).multiply(m23) //Compiletime error
System.out.println("5: " + Matrix.make(_3.class, _5.class).asString());
int n = 3;
new TypeParameter<_N>() {
public Class<? extends _N> getType() {
return _N.fromValue(n);
}
public <T extends _N> void useType(Class<T> three) {
Matrix<_7, T> m73 = Matrix.make(_7.class, three);
Matrix<_7, _4> m74 = m73.multiply(Matrix.make(three, _4.class));
System.out.println("6: " + m74.asString());
}
};
}
0: [
[ 0.0 1.0 2.0 ]
[ 2.0 3.0 4.0 ]
]
1: [
[ 0.0 1.0 ]
]
2: [
[ 0.0 1.0 2.0 0.0 0.0 0.0 0.0 ]
[ 2.0 3.0 4.0 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ]
]
3: [
[ 0.0 2.0 4.0 ]
[ 4.0 6.0 8.0 ]
]
4: [
[ 6.0 9.0 ]
[ 18.0 27.0 ]
]
5: [
[ 0.0 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 0.0 ]
]
6: [
[ 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 ]
[ 0.0 0.0 0.0 0.0 ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment