Skip to content
Snippets Groups Projects
Commit a3487d51 authored by fefe's avatar fefe
Browse files

Done the basics of matrix, .det() stil not passing tests

parent 790aca53
No related branches found
No related tags found
No related merge requests found
......@@ -3,29 +3,41 @@ import java.util.List;
import java.util.ArrayList;
import java.util.function.BiFunction;
import java.util.function.DoubleFunction;
import ch.hepia.numeric.Vector;
public class Matrix {
private List<Vector> col;
private Matrix(List<Vector> vecLst){
this.col = vecLst;
}
public int nbRows() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.col.get(0).len();
}
public int nbCols() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.cols().size();
}
public List<Vector> cols() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public List<Transposed> rows() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.col;
}
public List<Transposed> rows(){
List<Transposed> lstTran = new ArrayList<>();
for(int i = 0;i < this.nbRows();i++){
List<Double> newTran = new ArrayList<>();
for (Vector x : this.cols()){
newTran.add(x.get(i));
}
lstTran.add(new Transposed(newTran));
}
return lstTran;
}
public Transposed getRow(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.rows().get(i);
}
public Vector getCol(int i) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return cols().get(i);
}
public double get(int i, int j) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
return this.cols().get(i).get(j);
}
void set(int row, int col, double value) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
......@@ -37,7 +49,11 @@ public class Matrix {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Matrix mul(double d) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
List<Vector> lstVec = new ArrayList<>();
for (int i = 0; i < this.cols().size();i++){
lstVec.add(this.cols().get(i).mul(d));
}
return Matrix.of(lstVec);
}
public Matrix mul(Matrix that) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
......@@ -46,7 +62,11 @@ public class Matrix {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public boolean isSquare() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
if (this.nbCols() == this.nbRows())
{
return true;
}
return false;
}
public Matrix t() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
......@@ -58,7 +78,19 @@ public class Matrix {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public Matrix removed(int i, int j) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
List<Vector> lstVec = new ArrayList<>();
for (int x = 0; x < this.nbCols(); x++){
List<Double> newVec = new ArrayList<>();
for (int y = 0; y < this.nbRows(); y++){
if ( x != i && y != j){
newVec.add(this.get(x, y));
}
}
if (newVec.size() > 0){
lstVec.add(Vector.of(newVec));
}
}
return Matrix.of(lstVec);
}
public Matrix adjugate() {
......@@ -78,7 +110,7 @@ public class Matrix {
public double det() {
if (!isSquare()) {
/* do something smart */
//throw new Exception("");
}
if( nbCols() == 1) {
......@@ -86,11 +118,16 @@ public class Matrix {
} else if( nbCols() == 2 ){
return get(0,0)*get(1,1) - get(1,0)*get(0,1);
} else {
/* A vous de jouer */
throw new UnsupportedOperationException("This feature isn't implemented yet");
double tot = 0.0;
for (int i = 0; i < this.nbCols();i++){
if (i+this.nbCols() % 2 == 0){
tot += this.removed(i, 0).mul(this.get(i, 0)).det();
}
else{
tot -= this.removed(i, 0).mul(this.get(i, 0)).det();
}
}
return tot;
}
}
......@@ -100,18 +137,37 @@ public class Matrix {
@Override
public String toString() {
throw new UnsupportedOperationException("This feature isn't implemented yet");
String str = "";
for (Vector x : this.cols()){
str += "\n" + x.toString();
}
return str;
}
@Override
public boolean equals(Object obj) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
if (this == obj) return true;
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Matrix mat = (Matrix) obj;
if (mat.nbRows() != this.nbRows() || mat.nbCols() != this.nbCols()){
return false;
}
for (int i = 0; i < this.nbCols();i++){
for (int j = 0; j < this.nbRows();j++){
if (this.get(i, j) != mat.get(i, j)){
return false;
}
}
}
return true;
}
public static Matrix of(List<Transposed> ts) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
public static Matrix of(List<Vector> ts) {
return new Matrix(ts);
}
public static Matrix of(Transposed... ts) {
public static Matrix of(Vector... ts) {
throw new UnsupportedOperationException("This feature isn't implemented yet");
}
public static Matrix empty() {
......
......@@ -2,6 +2,8 @@ package ch.hepia.numeric;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
//import sun.jvmstat.monitor.VmIdentifier;
import java.util.List;
......@@ -144,4 +146,68 @@ class VectorTest {
assertEquals(Vector.fill(4).withValue(1.5), Vector.of(1.5, 1.5, 1.5, 1.5));
assertEquals(Vector.fill(2).withValue(1.0), Vector.of(1.0, 1.0));
}
@Test
void vectorLinespacev2() {
assertEquals(Vector.from(0.0).to(1.0).repeat(3), Vector.of(0.0, 0.5, 1.0));
assertEquals(Vector.from(0.0).to(1.0).repeat(5), Vector.of(0.0, 0.25, 0.5, 0.75, 1.0));
}
// Matrix
@Test
void matrixCreation() {
List<Vector> lstVec = new ArrayList<>();
lstVec.add(Vector.of(1.0,1.0,1.0));
lstVec.add(Vector.of(1.0,1.0,1.0));
lstVec.add(Vector.of(1.0,1.0,1.0));
Matrix mat = Matrix.of(lstVec);
assertEquals(Matrix.of(lstVec), Matrix.of(lstVec));
assertEquals(Matrix.of(lstVec), Matrix.of(lstVec));
assertEquals(Matrix.of(lstVec).nbCols(), 3);
assertEquals(Matrix.of(lstVec).nbRows(), 3);
}
@Test
void matrixRemoved() {
List<Vector> lstVec = new ArrayList<>();
lstVec.add(Vector.of(1.0,2.0,3.0));
lstVec.add(Vector.of(4.0,5.0,6.0));
lstVec.add(Vector.of(7.0,8.0,9.0));
Matrix mat = Matrix.of(lstVec);
lstVec = new ArrayList<>();
lstVec.add(Vector.of(5.0,6.0));
lstVec.add(Vector.of(8.0,9.0));
assertEquals(mat.removed(0, 0), Matrix.of(lstVec));
}
@Test
void matrixMul() {
List<Vector> lstVec = new ArrayList<>();
lstVec.add(Vector.of(1.0,4.0,7.0));
lstVec.add(Vector.of(2.0,5.0,8.0));
lstVec.add(Vector.of(3.0,6.0,9.0));
Matrix mat = Matrix.of(lstVec);
lstVec = new ArrayList<>();
lstVec.add(Vector.of(2.0,8.0,14.0));
lstVec.add(Vector.of(4.0,10.0,16.0));
lstVec.add(Vector.of(6.0,12.0,18.0));
assertEquals(mat.mul(2.0), Matrix.of(lstVec));
lstVec = new ArrayList<>();
lstVec.add(Vector.of(3.0,12.0,21.0));
lstVec.add(Vector.of(6.0,15.0,24.0));
lstVec.add(Vector.of(9.0,18.0,27.0));
assertEquals(mat.mul(3.0), Matrix.of(lstVec));
}
@Test
void matrixDet() {
List<Vector> lstVec = new ArrayList<>();
lstVec.add(Vector.of(1.0,4.0,7.0));
lstVec.add(Vector.of(2.0,5.0,8.0));
lstVec.add(Vector.of(3.0,6.0,9.0));
Matrix mat = Matrix.of(lstVec);
assertEquals(mat.det(), 0.0);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment