I've got 2 Java programs, with the aim of solving some matrix math that involves complex numbers (an EE problem). Both programs have main functions, the one with all the complex math operations has a main function to test the functions in that program itself. I rectified the issue of having an array of objects that have NULL
instead of a complex value by writing a function to specifically do so. My questions are:
Is there a better way to set object's values to zero(0+0i) instead of having to call my
non_nullify
function before almost every other function in the libraryAre there any other optimizations that can be done to make the program better in terms of modularity, performance, or other OOP terms
package com.PS1;
public class FormBus_builtin {
public static void main(String args[]) {
double[][] table = {{1,2,0,0.2},
{2,3,0,0.4},
{3,4,0,0.5},
{4,1,0,0.25}};
int rows = table.length;
int ne = rows;
int cols = table[0].length;
double[] sB_Array = column_extract(table,1);
double[] eB_Array = column_extract(table,2);
double[] resistanceArray = column_extract(table, 3);
double[] reactanceArray = column_extract(table, 4);
int sB,
eB;
double r,
x;
complex y_selfAdmittance,
impedance;
printArray(sB_Array,"starting bus array ");
printArray(eB_Array, "ending bus array");
printArray(resistanceArray, "resistance array");
printArray(reactanceArray,"reactance array");
complex[][] Y_bus = new complex[rows][cols];// creation of zero matrix for further substitution
for(int k=0;k<ne;k++) {
sB =(int) (sB_Array[k])-1;
eB =(int) (eB_Array[k])-1;
r = resistanceArray[k];
x = reactanceArray[k];
impedance = new complex(r,x);
y_selfAdmittance = complex.reciproc(impedance);
complex.println(y_selfAdmittance);
Y_bus[sB][eB] = complex.subtract(Y_bus[sB][eB],y_selfAdmittance);
Y_bus[eB][sB] = complex.subtract(Y_bus[eB][sB],y_selfAdmittance);
Y_bus[sB][sB] = complex.add(Y_bus[sB][sB],y_selfAdmittance);
Y_bus[eB][eB] = complex.add(Y_bus[eB][eB],y_selfAdmittance);
}
complex.printMatrix(Y_bus, "The bus admittance matrix is");
}
public static double[] column_extract(double a[][],int col) {
int rows = a.length;
int cols = a[0].length;
double[] res = new double[rows];
for(int i=0;i<rows;i++) {
res[i]=a[i][col-1];
}
if(col>cols)
System.out.println("Invalid column requested/matrix given ");
return res;
}
public static void printArray(double a[]) {
int length = a.length;
for(int i=0;i<length;i++)
System.out.println(a[i]+" ");
}
public static void printArray(double a[], String info) {
System.out.println(info);
printArray(a);
}
public static void printMatrix(double a[][]) {
int rows = a.length;
int cols = a[0].length;
for(int i = 0;i<rows;i++) {
for(int j = 0;j<cols;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void printMatrix(double a[][], String info) {
System.out.println(info);
int rows = a.length;
int cols = a[0].length;
for(int i = 0;i<rows;i++) {
for(int j = 0;j<cols;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
Program for complex functions:
package com.PS1;
import java.lang.Math;
public class complex {
double r,i;
public complex(double its_r, double its_i) {
this.r = its_r;
this.i = its_i;
}
public complex(int its_r, int its_i){
this.r = (double) its_r;
this.i = (double) its_i;
}
public complex(int its_r,double its_i){
this.r = (float) its_r;
this.i = its_i;
}
public static complex non_nullify(complex n1){
complex n = new complex(0, 0);
return n;
}
public static complex nullCheck(complex n1) {
if(n1==null)
n1 = complex.non_nullify(n1);
return n1;
}
public static complex add(complex n1, complex n2) {
n1=nullCheck(n1);
n2=nullCheck(n2);
complex sum = new complex(0,0);
sum.r = n1.r+n2.r;
sum.i = n1.i+n2.i;
return sum;
}
public static complex subtract(complex n1,complex n2) {
n1=nullCheck(n1);
n2=nullCheck(n2);
complex diff = new complex(0, 0);
diff.r = n1.r-n2.r;
diff.i = n1.i-n2.i;
return diff;
}
public static complex mul(complex n1, complex n2) {
n1=nullCheck(n1);
n2=nullCheck(n2);
complex prod = new complex(0, 0);
prod.r = (n1.r*n2.r) - (n1.i*n2.i);
prod.i = (n1.r*n2.i) + (n2.r*n1.i);
return prod;
}
public static complex div(complex n1, complex n2) {
n1=nullCheck(n1);
n2=nullCheck(n2);
complex div_res = new complex(0,0);
double denom = mod(n2);
div_res.r = (float) (((n1.r*n2.r)+(n1.i*n2.i))/Math.pow(denom,2));
div_res.i = (float) (((n2.r*n1.i)-(n1.r*n2.i))/Math.pow(denom,2));
return div_res;
}
public static complex conj(complex n1) {
n1=nullCheck(n1);
complex conjug = new complex(0, 0);
conjug.r = n1.r;
conjug.i = -n1.i;
return conjug;
}
public static double mod(complex n1) {
n1=nullCheck(n1);
double modulus = Math.sqrt(Math.pow(n1.r, 2)+Math.pow(n1.i, 2));
return modulus;
}
public static complex reciproc(complex n1) {
n1=nullCheck(n1);
complex conjugate = conj(n1);
complex reciproc = new complex(0, 0);
reciproc.r = (float) ((conjugate.r)/Math.pow(mod(n1), 2));
reciproc.i = (float) ((conjugate.i)/Math.pow(mod(n1), 2));
return reciproc;
}
public static void print(complex n1) {
n1=nullCheck(n1);
System.out.printf("%.1f + %.1fi", n1.r,n1.i);
}
public static void println(complex n1) {
n1 = nullCheck(n1);
System.out.printf("%.1f + %.1fi\n", n1.r,n1.i);
}
public static void print(complex n1, String info) {
System.out.println(info);
print(n1);
}
public static void printMatrix(complex a[][]) {
int rows = a.length;
int cols = a[0].length;
System.out.print('[');
for(int i = 0;i<rows;i++) {
for(int j = 0;j<cols;j++) {
complex.print(a[i][j]);
if(j<cols-1)
System.out.print(", ");
else
System.out.print(';');
}
if(i<rows-1);
else
System.out.print(']');
System.out.println();
}
}
public static void printMatrix(complex a[][], String info) {
System.out.println(info);
printMatrix(a);
}
public static void main(String args[]) {
complex n1 = new complex(2, 3);
complex n2 = new complex(3, 4);
complex productComplex = new complex(0, 0);
productComplex = mul(n1,n2);
print(productComplex);
complex divComplex = new complex(0, 0);
divComplex = div(productComplex,n1);
print(divComplex);
complex reciprocal = new complex(0, 0);
complex n3 = new complex(0, 0.2);
reciprocal = reciproc(n3);
print(reciprocal,"reciprocal");
}
}