Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/project.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3 4
1+2i -1.5-1.1i 2.12 91+5i
-1+3i 1.2+3.5i -3.3 1+15i
12.31 1.3-5i 12.3i -78.3i
3 changes: 3 additions & 0 deletions out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
6.7334-22.9975i
-1.7976+2.084i
15.6994+7.396i
151 changes: 151 additions & 0 deletions src/solver/AugmentedMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package solver;

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;


public class AugmentedMatrix {
private Row[] matrix;
private String decimalPattern = "#.####";

// We don't always need swapHistory, so method which uses this field must check whether it is initialized or not.
private ArrayList<SwapInfo> swapHistory;


AugmentedMatrix(Complex[][] matrix) {
setMatrix(matrix);
}

AugmentedMatrix() {}

public void readMatrix(Scanner scanner) throws InputMismatchException {
int n = scanner.nextInt();
int m = scanner.nextInt();
matrix = new Row[n];
for (int i = 0; i < n; i++) {
matrix[i] = new Row(m);
for (int j = 0; j < m; j++) {
this.set(i, j, new Complex(scanner.next()));
}
}
}

public void set(int i, int j, Complex value) {
matrix[i].set(j, value);
}

public Complex get(int i, int j) {
return matrix[i].get(j);
}


public String fancyPrint(String decimalPattern) {
if(decimalPattern == null) {
decimalPattern = this.decimalPattern;
}
StringBuilder sb = new StringBuilder();
Complex[] row;
for (int i = 0; i < matrix.length; i++) {
row = matrix[i].getRow();
sb.append("[");
for(int j =0; j < row.length; j++) {
row[j].setDecimalPattern(decimalPattern);
sb.append(row[j].toString());
sb.append(", ");
}
sb.delete(sb.length()-2, sb.length());
sb.append("]");
sb.append("\n");
}
return sb.toString();
}

@Override
public String toString() {
return fancyPrint(null);
}

public void swapRows(int firstRowIndex, int secondRowIndex) {
Row temp = matrix[firstRowIndex];
matrix[firstRowIndex] = matrix[secondRowIndex];
matrix[secondRowIndex] = temp;
}

private void addSwapHistoryEntry(int prevIndex, int nextIndex) {
// Initialization check
if(swapHistory==null) {
swapHistory = new ArrayList<>();
}
swapHistory.add(new SwapInfo(prevIndex, nextIndex));
}

public void clearSwapHistory() {
swapHistory.clear();
}

public void swapColumns(int firstColumnIndex, int secondColumnIndex) {
swapColumns(firstColumnIndex, secondColumnIndex, true);
}

public void swapColumns(int firstColumnIndex, int secondColumnIndex, boolean writeLog) {
Complex coeff1, coeff2;
for(Row row : matrix) {
coeff1 = row.get(firstColumnIndex);
coeff2 = row.get(secondColumnIndex);
row.set(firstColumnIndex, coeff2);
row.set(secondColumnIndex, coeff1);
}
if(writeLog) {
addSwapHistoryEntry(firstColumnIndex, secondColumnIndex);
}
}

public ArrayList<SwapInfo> getSwapHistory() {
return swapHistory;
}

public Complex[][] getMatrixCopy() {
Complex[][] copy = new Complex[matrix.length][];
for(int i = 0; i < matrix.length; i++) {
copy[i] = matrix[i].getRow();
}
return copy;
}

public void setMatrix(Complex[][] matrix) {
this.matrix = new Row[matrix.length];
for(int i = 0; i < matrix.length; i++) {
this.matrix[i] = new Row(matrix[i]);
}
}

public Row getRow(int index) {
return this.matrix[index];
}

public Row getColumnCopy(int index) {
int rows = size()[0]; // Number of rows
Row col = new Row(rows);
for(int i = 0; i < rows; i++) {
col.set(i, matrix[i].get(index));
}
return col;
}



/**
* Get dimensions of Augmented matrix
* @return size()[0] - number of rows, size()[1] - number of columns
*/
public int[] size() {
int[] size = new int[2];
size[0] = matrix.length;
// Consider that all rows have the same size, according to the class initialization
size[1] = matrix[0].size();
return size;
}


}
123 changes: 123 additions & 0 deletions src/solver/Complex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package solver;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Complex {
private final double re;
private final double im;
private String decimalPattern = "#.####";

public static final Complex ZERO = new Complex(0, 0);

public Complex(double real, double imag) {
this.re = real;
this.im = imag;
}

public Complex(String str) {
Complex cmp = parseComplex(str);
this.re = cmp.re;
this.im = cmp.im;
}

public Complex(Complex b) {
this.re = b.re;
this.im = b.im;
}

@Override
public boolean equals(Object x) {
if (x == null) return false;
if (this.getClass() != x.getClass()) return false;
Complex that = (Complex) x;
return (this.re == that.re) && (this.im == that.im);
}



public Complex add(Complex b) {
Complex a = this;
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}

public Complex subtract(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}

public Complex multiply(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.im * b.re + a.re * b.im;
return new Complex(real, imag);
}

public Complex reciprocal() {
Complex a = this;
double denominator = a.re * a.re + a.im * a.im;
double real = a.re / denominator;
double imag = - a.im / denominator;
return new Complex(real, imag);
}

public Complex divide(Complex b) {
Complex a = this;
return a.multiply(b.reciprocal());
}

public static Complex parseComplex(String inp) {
Pattern imagPattern = Pattern.compile("([+-]?\\d+(\\.?\\d+)?([eE][+-]?\\d)?[i|I])|([+-]?[i|I](\\d+(\\.?\\d+)?)?([eE][+-]?\\d)?)");

// My numerous attempts, i leave it here)

// Pattern realPattern = Pattern.compile("([+-]?(?!i|I)?\\d++(\\.?\\d++)?([eE][+-]?\\d)?(?!i|I))");
// Pattern realPattern = Pattern.compile("(?!(([+-]?\\d+(\\.?\\d+)?([eE][+-]?\\d)?[i|I])))");
// Matcher re = realPattern.matcher(inp);
// re.find();
// System.out.printf("Re: " + re.group() + " ");


Matcher im = imagPattern.matcher(inp);
String impart, repart;
try {
im.find();
impart = im.group();
} catch (IllegalStateException e) {
impart = ""; // If a repart or impart is blank it will be zero
}
repart = inp.replace(impart, ""); // It is easier than finding/creating regexp pattern to find real part

if(impart.equals("+i") || impart.equals("+I") || impart.equals("i") || impart.equals("I")) {
impart = "1i";
} else if(impart.equals("-i") || impart.equals("-I")) {
impart = "-1i";
}

double rep = repart.isBlank() ? 0 : Double.parseDouble(repart);
double imp = impart.isBlank() ? 0 : Double.parseDouble(impart.replaceAll("i|I", ""));

return new Complex(rep, imp);
}

public void setDecimalPattern(String pattern) {
this.decimalPattern = pattern;
}

@Override
public String toString() {
String ans;
DecimalFormat f = new DecimalFormat(decimalPattern);
if (im == 0) return f.format(re) + "";
if (re == 0) return f.format(im) + "i";
if (im < 0) return f.format(re) + "-" + f.format(-im) + "i";
return f.format(re) + "+" + f.format(im) + "i";
}


}
Loading