Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
algo
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner Scan = new Scanner("System.in");
Matrix a = new Matrix(3,3);
Matrix b = new Matrix(3,3);
//Linked list ? Array List
a.randomize();
a.print();
System.out.println(" ");
b.randomize();
b.print();
System.out.println("Matrix a");
a.print();
System.out.println(a.times(a,b));
System.out.println(a.divide(a,b));
// System.out.println(Matrix.divide(a,b));
}
}
package com.company;
import org.jetbrains.annotations.NotNull;
public class Matrix {
// Scanner sc = new Scanner("System.in");
//fill +
//displaySize +
//+, - , * ,*
//inverseMatrix
//det
//transpose
//isSameSize? +
private int[][] matrix;
private int n;
private int m;
public Matrix(int n,int m){
this.n = n;
this.m = m;
this.matrix = new int[n][m];
}
public Matrix(int n,int m,int[][] matrix){
this.n = n;
this.m = m;
this.matrix = matrix;
}
public Matrix(int n, int m, double[][] matrix) {
}
public static Matrix toObject(int n,int m, int[][] matrix){
return new Matrix(n,m,matrix);
}
public static Matrix toObject(int n, int m, double[][] matrix){
return new Matrix(n, m, matrix);
}
public void displaySize(){
System.out.println(this.n + " x " + this.m);
}
public int[][] getMatrix() {
return matrix;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public int getM() {
return m;
}
public void setM(int m) {
this.m = m;
}
static int[][] swap(int [][]arr, int i1, int j1, int i2, int j2)
{
int temp = arr[i1][j1];
arr[i1][j1] = arr[i2][j2];
arr[i2][j2] = temp;
return arr;
}
public void randomize(){
System.out.println("Filling array with random n-s: ");
for (int i = 0; i < this.n ; i++) {
for (int j = 0; j < this.m; j++) {
this.matrix[i][j] = (int)((Math.random())*10);
}
}
}
public void print(){
System.out.println("Displaying your array: ");
for (int i = 0; i < this.n ; i++) {
System.out.println(" ");
for (int j = 0; j < this.m; j++) {
System.out.print(this.matrix[i][j] + " ");
}
}
System.out.println("");
}
public static boolean isSameSize(Matrix a ,Matrix b){
return a.n == b.n && a.m == b.m;
}
public static boolean isSquareMatrix(Matrix obj){
if(obj.n == obj.m) return true;
else return false;
}
public static int getDim(Matrix obj){
int dim = 0;
if(isSquareMatrix(obj)){
dim = obj.n;
}
return dim;
}
public static boolean isInvertible(Matrix obj){
float det = determinantOfMatrix(obj);
if (det != 0) return true;
else return false;
}
public static double[][] copyMatrix(Matrix obj) {
double[][] a = new double[obj.n][obj.m];
for (int i = 0; i < obj.n; i++) {
for (int j = 0; j < obj.m; j++) {
a[i][j] = obj.matrix[i][j];
}
}
return a;
}
public static int determinantOfMatrix(Matrix obj)
{
int n = getDim(obj);
int num1, num2, det = 1, index, total = 1;
int []temp = new int[n + 1];
for(int i = 0; i < n; i++)
{
index = i;
while(obj.matrix[index][i] == 0 && index < n)
{
index++;
}
if(index == n)
{ continue;
}
if(index != i)
{ for(int j = 0; j < n; j++)
{
swap(obj.matrix, index, j, i, j);
}
det = (int) (det*Math.pow(-1, index - i));
}
for(int j = 0; j < n; j++)
{
temp[j] = obj.matrix[i][j];
}
for(int j = i + 1; j < n; j++)
{
num1 = temp[i];
num2 = obj.matrix[j][i];
for(int k = 0; k < n; k++)
{
obj.matrix[j][k] = (num1 * obj.matrix[j][k]) - (num2 * temp[k]);
}
total = total * num1;
}
}
for(int i = 0; i < n; i++)
{
det = det * obj.matrix[i][i];
}
return (det/total);
}
@NotNull
public static Matrix plus(Matrix a, Matrix b){
int[][] temp = new int[a.n][a.m];
if(isSameSize(a,b)){
for (int i = 0; i < a.n ; i++) {
for (int j = 0; j < a.m ; j++) {
temp[i][j] = a.matrix[i][j] + b.matrix[i][j];
}
}
Matrix obj = new Matrix(a.n,a.m,temp);
return obj;
}
else throw new IllegalArgumentException("Size of is not equal");
}
public static Matrix minus(Matrix a, Matrix b){
int[][] temp = new int[a.n][a.m];
if(isSameSize(a,b)){
for (int i = 0; i < a.n ; i++) {
for (int j = 0; j < a.m ; j++) {
temp[i][j] = a.matrix[i][j] - b.matrix[i][j];
}
}
Matrix obj = new Matrix(a.n,a.m,temp);
return obj;
}
else throw new IllegalArgumentException("Size of is not equal");
}
public static Matrix times(Matrix a,Matrix b ){
//O(n^3)
double[][] temp = new double[a.n][b.m];
if((a.m==b.n) || (isSquareMatrix(a)&&isSquareMatrix(b))) {
//
for (int i = 0; i < a.n ; i++) {
for (int j = 0; j < b.m ; j++) {
for (int k = 0; k < b.n ; k++) {
temp[i][j] += a.matrix[i][k] * b.matrix[k][j];
}
}
}
return new Matrix(a.n,b.m,temp);
}
else throw new IllegalArgumentException("Error! Columns of A != Rows of B");
}
public static double[][] invert(Matrix obj)
{
if(isInvertible(obj)) {
double[][] a = copyMatrix(obj);
int n = obj.matrix.length;
double[][] x = new double[n][n];
double[][] b = new double[n][n];
int[] index = new int[n];
for (int i = 0; i < n; ++i)
b[i][i] = 1;
gaussian(a,obj);
for (int i = 0; i < n - 1; ++i)
for (int j = i + 1; j < n; ++j)
for (int k = 0; k < n; ++k)
b[index[j]][k] -= a[index[j]][i] * b[index[i]][k];
for (int i = 0; i < n; ++i) {
x[n - 1][i] = b[index[n - 1]][i] / a[index[n - 1]][n - 1];
for (int j = n - 2; j >= 0; --j) {
x[j][i] = b[index[j]][i];
for (int k = j + 1; k < n; ++k) {
x[j][i] -= a[index[j]][k] * x[k][i];
}
x[j][i] /= a[index[j]][j];
}
}
return x;
}
else throw new IllegalArgumentException("Matrix not invertible");
}
public static void gaussian(double[][] a,Matrix obj)
{
int b = obj.matrix.length;
int[] index = new int[b];
int n = index.length;
double[] c = new double[n];
for (int i=0; i<n; ++i)
index[i] = i;
for (int i=0; i<n; ++i)
{
double c1 = 0;
for (int j=0; j<n; ++j)
{
double c0 = Math.abs(a[i][j]);
if (c0 > c1) c1 = c0;
}
c[i] = c1;
}
int k = 0;
for (int j=0; j<n-1; ++j)
{
double pi1 = 0;
for (int i=j; i<n; ++i)
{
double pi0 = Math.abs(a[index[i]][j]);
pi0 /= c[index[i]];
if (pi0 > pi1)
{
pi1 = pi0;
k = i;
}
}
int itmp = index[j];
index[j] = index[k];
index[k] = itmp;
for (int i=j+1; i<n; ++i)
{
double pj = a[index[i]][j]/a[index[j]][j];
a[index[i]][j] = pj;
for (int l=j+1; l<n; ++l)
a[index[i]][l] -= pj*a[index[j]][l];
}
}
}
public static Matrix divide(Matrix a,Matrix b ){
//O(n^3)
if(isSameSize(a,b)){
double[][] temp = invert(b);
Matrix res = toObject(b.n,b.m,temp);
return times(a,res);
}
else throw new IllegalArgumentException("Error! Columns of A != Rows of B");
}
}
1.
У нас есть два двумерных массива - объекты класса Main
нужно создать и передать массивы вместе с K и N
Как это сделать?
1. использовать дженерик <Т> для всех типов массивов string, double, int, char etc..
2. прописать вручную все геттеры и сеттеры и создать кучу двумерных массивов в классе
Matrix
3.
Проблемы:
1. Передача массивов в класс и обратно
2.
Инверс матрицы это А * А^-1 = Е
Перегружение операторов
гаусса жардано
/* public void fill(){
System.out.println(" Fill array with num - s : ");
for (int i = 0; i < this.n ; i++) {
for (int j = 0; j < this.m; j++) {
this.matrix[i][j] = sc.nextInt();
}
}
}*/
Что делаю:
1. делаю геттер и сеттер для каждого массива вместе с n и m
//перегрузить методы
public static double sum(double[][] matA,double[][] matB){
double sum = 0;
return sum;
}
public static int sum(int[][] matA, int[][] matB){
int sum=0;
return sum;
}
public static double "*"(double[][] matA,double[][] matB){
double sub = 0;
return sub;
}
public static int subtraction(int[][] matA, int[][] matB){
int sub = 0;
return sub;
}
public static double multiplication(double[][] matA,double[][] matB){
double mult = 0;
return mult;
}
public static int multiplication(int[][] matA,int[][] matB){
int mult = 0;
return mult;
}
public static double division(double[][] matA,double[][] matB){
double div = 0;
return div;
}
public static int division(int[][] matA,int[][] matB){
int div = 0;
return div;
}
public static int[][] getMatA() {
return matA;
}
public static void setMatA(int[][] matA) {
Matrix.matA = matA;
}
public static int[][] getMatB() {
return matB;
}
public static void setMatB(int[][] matB) {
Matrix.matB = matB;
}
/*
@NotNull
public static Matrix getMinor(Matrix obj){
int n = obj.matrix.length;
int[][] minor = new int[n-1][n-1];
int r = 0, s = 0;
for (int i = 0; i < n; i++) {
int[] row = obj.matrix[i];
if (i != obj.n) {
for (int j = 0; j < row.length; j++) {
if (j != obj.m) {
minor[r][s++] = row[j];
}
}
s=0;
r++;
}
}
return toObject(r,s,minor);
}
public static double det(Matrix obj){
if(obj.n==1) return obj.matrix[0][0];//sum all this elements to an object
if(isSquareMatrix(obj)){
if(obj.n == 2 && obj.m==2){
int det=0;
det = (obj.matrix[0][0]* obj.matrix[1][1] )- (obj.matrix[0][1]*obj.matrix[1][0]);
return det;
}
/*
int[][] temp = new int[obj.n][obj.m];
int[] vec = new int[obj.n];
for (int i = 0; i < obj.n; i++) {
vec = temp[i];
}*/
/*
double determinant = 0;
for (int j = 0; j < obj.n; j++) {
determinant += Math.pow(-1, j) * obj.matrix[0][j] * det(getMinor(obj));
}
return determinant;
}
else throw new IllegalArgumentException("Matrix is not squared");
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.