Question

In: Computer Science

im trying to write a java code that take a matrix of vector and fine it's...

im trying to write a java code that take a matrix of vector and fine it's inverse (the the inverse in linear algebra) then multiple this matrix with a vector to fine other vector

(matrix)-1 × ( vector ) = (vector)

Solutions

Expert Solution

Hope this helps

import java.util.Scanner;
class newclass
{
public static void main(String argv[])
{
Scanner sc = new Scanner(System.in);
System.out.println("dimension of square matrix: ");
int n = sc.nextInt();
double a[][]= new double[n][n];
System.out.println("elements of matrix: ");
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i][j] = sc.nextDouble();

double d[][] = invert(a);

System.out.println("Inverse of matrix is: ");
for (int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
System.out.print(d[i][j]+" ");
}
System.out.println();
}
double res[][] = new double[n][1] ;
double m[][] = new double[n][1] ;
System.out.println("matrix to multiply of deimension "+ n+"*1");
for(int i=0;i<n;i++)
{
for(int j=0;j<1;j++)
{
m[i][j]=sc.nextDouble();
}
}
double sum=0;
int ma,na,qa,pa;
ma=n;
na=n;
pa=n;
qa=1;
for (int c = 0; c < ma; c++)
{
for (int da = 0; da < qa; da++)
{
for (int k = 0; k < pa; k++)
{
sum = sum + d[c][k]*m[k][da];
}
res[c][da] = sum;
sum = 0;
}
}
System.out.println("mutliplication of matrix with given vector :");
for (int c = 0; c < n; c++)
{
for (int q = 0; q < 1; q++)
System.out.print(res[c][q]+"\t");

System.out.print("\n");
}

}  

  
public static double[][] invert(double a[][])
{
int n = a.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, index);

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;
}


public static void gaussian(double a[][], int index[])
{
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];
}
}
}
}


Related Solutions

I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
Im trying to get a GUI interface in java where there is four text fields for...
Im trying to get a GUI interface in java where there is four text fields for the first name, last name, department, and phone number of employee in a program. Then also have a radio button below for Gender (Male/Female/Other) and a list for the Title (Mr./Ms./Mrs./Dr./Col./Prof.). At the very bottom of the frame there has to be buttons for printing, submitting and exiting but for whatever reason when I tried it nothing appears in the frame regardless of what...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
maximumST is a function that should take an n × n vector (representing the adjacency matrix...
maximumST is a function that should take an n × n vector (representing the adjacency matrix of a graph) as input and return the value of the maximum spanning tree of the graph defined by the n × n vector. Code: #include <vector> #include <cmath> #include <iostream> #include <cstdio> using namespace std; double maximumST( vector< vector<double> > adjacencyMatrix ){   vector<int> row(4,-1);   vector< vector<int> > matrix(5,row);   cerr << matrix.size() << endl;   cerr << matrix[0].size() << endl;   for( int i = 0;...
I get an error when im trying to run this java program, I would appreciate if...
I get an error when im trying to run this java program, I would appreciate if someone helped me asap, I will make sure to leave a good review. thank you in advance! java class Node public class Node { private char item; private Node next; Object getNext; public Node(){    item = ' '; next = null; } public Node(char newItem) { setItem(newItem); next = null; } public Node(char newItem, Node newNext){ setItem(newItem); setNext(newNext); } public void setItem(char newItem){...
This code works fine. In java, can you: change the variable and method names to make...
This code works fine. In java, can you: change the variable and method names to make logical sense add method header comments to describe what the method does and what the params/returns are add a class header comment to describe what the code does import java.util.Arrays; import java.util.Scanner; public class MethodLibrary { public static void main(String args[]) { System.out.println("MethodLibrary"); System.out.println("Example use of methods"); int[] smallNums1 = {2, 1, 7, 4, 3}; display("display:", smallNums1); char [] characters = {'a','b','c','d','e','f'}; first( characters,...
Write a java program that calculates a speeding fine. The user is prompted for the speed...
Write a java program that calculates a speeding fine. The user is prompted for the speed of the vehicle, the posted speed limit and if the offence took place in the construction zone. The fine is calculated at $75 + 3$/ km over the speed limit for the first 20km over + $6 / km for the next 20 and $9/km after that. If the posted limit is 40km, the fine is doubled. If the offence took place in a...
XML and XSL I'm trying to style my XML code but it's not implementing the XSL...
XML and XSL I'm trying to style my XML code but it's not implementing the XSL file when I run it even though the file is referenced. Any help? XML code: <?xml version="1.0"?> <?xml-stylesheet href="textbooks.xsl" type="text/xsl" ?> <textbooks> <textbook> <title> Introduction to Design and Analysis of Algorithms</title> <authors> <author> <firstName>Anany</firstName> <lastName>Levitin</lastName> </author> </authors> <publisher> <name>Ed, Pearson</name> <website>https://www.pearson.com</website> </publisher> <Year-of-Publication>2011</Year-of-Publication> <ISBN>978-0132316811</ISBN> <book-specific-website></book-specific-website> <edition>3rd edition</edition> <cover-type>Paperback</cover-type> </textbook> <textbook> <title>Software Engineering: A Practitioner’s Approach</title> <authors> <author> <firstName>Roger</firstName> <lastName>Pressman</lastName> </author> </authors> <publisher> <name>Ed, McGraw-Hill</name>...
C++ How to make this code take a class object instead of int for the vector...
C++ How to make this code take a class object instead of int for the vector queue and be able to enqueue and dequeue the object? #include <iostream> #include <float.h> #include <bits/stdc++.h> using namespace std; void print_queue(queue<int> Q) //This function is used to print queue { while (!Q.empty()) { cout<< Q.front() << " "; Q.pop(); } } int main() { int n = 10; vector< queue<int> > array_queues(n); //Create vector of queues of size 10, each entry has a queue...
It's java file 1. declare an vector without specifying the size 2. use push_back to add...
It's java file 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector 3. display the smallest, largest, and average of the numbers in the vector
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT