I just need to insert rows and columns of zeros in a matrix
tried to get exmples from web searches
ArrayList<double [][]> al = new ArrayList<>();
for (int i1= 0; i1<=i; i1++)
al.add(k);
System.out.println(al.size());
double[][] firstMatrix = al.get(i);
double [][] matrix1 = al.get(i);
double [][] matrix2 = al.get(i);
Matrix Matrix1 = new Matrix (matrix1);
Matrix1.show();
System.out.println();
Matrix1.insertColumn(0,0.0, 0.0, 0.0, 0.0);
Matrix1.show();
System.out.println();
Your question boils down to adding a new column/row to a Java array. This is not possible per se, because your array has an exact memory area allocated to it and you would need a larger memory area for the resulting array, so, the solution is to create a new array and solve the problem that way:
Adding a row
double[][] addRow(double[][] input, int where) {
double[][] output = new double[input.length + 1][input[0].length];
for (int rIndex = 0; rIndex < output.length; rIndex++) {
for (int cIndex = 0; cIndex < output[0].length; cIndex++) {
if (rIndex < where) output[rIndex][cIndex] = input[rIndex][cIndex];
else if (rIndex == where) output[rIndex][cIndex] = 0;
else output[rIndex][cIndex] = input[rIndex - 1][cIndex];
}
}
return output;
}
Adding a column
double[][] addColumn(double [][]input, int where) {
double[][] output = new double[input.length][input[0].length + 1];
for (int rIndex = 0; rIndex < output.length; rIndex++) {
for (int cIndex = 0; cIndex < output[0].length; cIndex++) {
if (cIndex < where) output[rIndex][cIndex] = input[rIndex][cIndex];
else if (cIndex == where) output[rIndex][cIndex] = 0;
else output[rIndex][cIndex] = input[rIndex][cIndex - 1];
}
}
return output;
}
and make sure you assign the result of these methods to whatever variable/member you have used as the data source.
Related
When I run my program for a histogram, it runs, but it doesn't give the output I want. I want
the output to be this, but I get this instead. If possible, I also want my histogram printed to output vertically, not horizontally. Furthermore, I would also like to add more bins.
PS: The data.txt file I have contains the following.
My code is:
https://pastebin.com/VyREurFP
BigInteger range = max.toBigInteger().subtract(min.toBigInteger());
BigInteger maxWidth = new BigInteger("8");
BigInteger width = new BigInteger("1");
if (range.compareTo(maxWidth) == 1){
width = range.divide(maxWidth);
if (range.remainder(maxWidth).compareTo(BigInteger.ZERO)==1){
width = width.add(BigInteger.ONE);
}
}
int binCount = range.divide(width).intValue();
if (range.remainder(width).compareTo(BigInteger.ZERO)==1){
binCount++;
}
// binCount is the number of bins that will be used
// Find the class interval for each bin
List<String> classInterval = new ArrayList<>();
BigInteger classStart = min.toBigInteger();
for (int b = 0; b< binCount; b++){
classInterval.add(String.format("%3s",classStart.toString())+" >= x < "+String.format("%3s",classStart.add(width).toString()));
classStart = classStart.add(width);
}
// Fill each bin
long[] bin = new long[binCount];
BigDecimal bw = new BigDecimal(width);
for (int i = 0; i< list.size(); i++){
int binNumber = list.get(i).divide(bw,RoundingMode.HALF_UP).intValue()-1;
bin[binNumber]++;
}
// Print out the details of each bin
int total = 0;
for (int b = 0; b< binCount; b++){
System.out.print(classInterval.get(b)+" |");
for (int d = 0; d<total; d++){
System.out.print("#");
}
for (int v = 0; v< bin[b]; v++){
System.out.print("*");
total++;
}
for (long s = bin[b]; s<60;s++){
System.out.print(" ");
}
System.out.println("("+bin[b]+")");
}
Here is the code for the classic rod cutting problem. As the code stands, the sizes are 1, 2, 3, and 4, the size of the price array arr[]. How would I modify the code so that the sizes are set to different values other than those given. For example, 1, 2, 3 and 5 instead.
static double cutRod(double price[],int n)
{
double val[] = new double[n+1];
val[0] = 0;
for (int i = 1; i<=n; i++)
{
double max_val = Integer.MIN_VALUE;
for (int j = 0; j < i; j++)
max_val = Math.max(max_val,
price[j] + val[i-j-1]);
val[i] = max_val;
}
return val[n];
}
public static void main(String args[])
{
double arr[] = new double[] {1.2, 3, 5.8, 10.1};
int size = arr.length;
System.out.println("Maximum Obtainable Value is " +
cutRod(arr, size));
}
}
In the classical problem, j+1 in your inner loop represents the cut sizes possible. If you have custom cut sizes, store those in an array and use them instead of j + 1.
In the above program, let custom_sizes be the array storing the cuts, eg. 1,2,3,5, etc.
Modifying the above program:
static double cutRod(double price[],int custom_sizes[], int n)
{
double val[] = new double[n+1];
val[0] = 0;
for (int i = 1; i<=n; i++)
{
double max_val = Integer.MIN_VALUE;
for (int j = 0; j < custom_sizes.length; j++) {
if (i - custom_sizes[j] >= 0)
max_val = Math.max(max_val,
price[j] + val[i-custom_sizes[j]]);
}
val[i] = max_val;
}
return val[n];
}
Please note that it is assumed that input is valid and that price and custom_sizes are of equal length = n.
I have written a series of matrix operations where I take a 2 dimensional float array, treat it as a matrix, and perform matrix operations on it to acquire an inverse. My problem has been that although the array I am using with the class methods isn't part of the class, every time I run the method with the array as the parameter, the array itself also becomes modified.
First I will describe how I got the inverse of my matrix and then I will show the output.
The steps to taking the inverse of a matrix are as follows:
Get the cofactor matrix (i.e. create a matrix of matrix minors of the original matrix and then negate every other entry. If C = Cofactor Matrix, M = Matrix of Minors, i is the current row, and j is the current column, then C[ i ][ j ] = M[ i ][ j ]*( -1 )^( i + j )
Convert the cofactor matrix to the adjugate (also known as adjoint) matrix by transposing (replacing row, column entry by its analogous column, row entry and vice versa) the cofactor matrix. If C = Cofactor Matrix, A = Adjugate Matrix, i is the current row, and j is the current column, then A[ i ][ j ] = C[ j ][ i ]
Finally, take one over determinant of the original matrix and multiply the adjugate matrix by that value. If I = Inverse Matrix, A = Adjugate Matrix and D = Determinant, then I = (1/D)*A
In order to test if you have truly acquired the Matrix Inverse of a Matrix, one can multiply the original matrix by its inverse to get the identity matrix.
If I = Inverse, O = Original Matrix, and id = Identity Matrix then O*I = id
Now I will present the code where I implement these operations. For the sake of conciseness, I will not describe how to get the Matrix of Minors or the Determinant, but the problem I have been encountering will become apparent anyways.
public class MatrixOperations {
//Note: this method works fine. There are no problems.
public float determinant(float [][] a)
{
float [][] temp_mat;
float res = 0;
//assuming a square matrix
/*If it's a 2X2, then use the formula for a determinant of
2X2 matrices.*/
if(a.length == 2)
{
return a[0][0]*a[1][1]-a[0][1]*a[1][0];
}
/*Otherwise do the determinant formula recursively until your
determinant is made up of 2X2 matrix determinants and scalar products*/
else
{
temp_mat = new float[a.length-1][a.length-1];
int placej = 0;
int placei = 0;
for(int k = 0; k<a.length;k++)
{
for(int j = 0; j<a.length; j++)
{
for(int i = 1; i < a.length; i++)
{
placei = i-1;
if(j != k)
{
if(j < k)
{
temp_mat[placei][j] = a[i][j];
}
else if(j > k)
{
if (i == 1){
placej = j-1;
}
temp_mat[placei][placej] = a[i][j];
}
}
}
}
res+=a[0][k]*determinant(temp_mat)*(int)Math.pow(-1, k);
}
return res;
}
}
//Note: this method also works fine
//Scalar product method
public float[][] mul(float[][] m, float r)
{
float[][] res = new float[m.length][m.length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
res[i][j]= m[i][j]*r;
}
}
return res;
}
//Note: This method also works fine
public float[][] mul(float[][] m,float[][] n)
{
float[][] res = new float[m.length][m.length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
for(int k = 0; k < m.length; k++)
{
res[i][j] += m[i][k]*m[k][i];
}
}
}
return res;
}
//The method for creating a matrix of minors
//Here I start having problems
public float[][] minor(float [][] m)
{
float [][] minor_mat = new float [m.length][m.length];
//If the matrix is greater than a 2X2, use this to generate a matrix of minors
if(m.length > 2)
{
float [][] current_minor = new float [m.length-1][m.length-1];
int placei = 0;
int placej = 0;
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
for(int k = 0; k < m.length; k++)
{
for(int l = 0; l < m.length; l++)
{
if(i != k && j != l)
{
if(k<i)
placei = k;
else if(k>i)
placei = k-1;
if(l<j)
placej = l;
else if(l>j)
placej = l-1;
current_minor[placei][placej] = m[k][l];
}
}
}
minor_mat[i][j] = this.determinant(current_minor);
}
}
}
//otherwise use the definition for 2X2 matrix of minors
else
{
//even though minor_mat is using m.clone() rather than m, when I return the result, m has still been modified for some reason.
minor_mat = m.clone()
float temp;
temp = minor_mat[0][0];
minor_mat[0][0] = minor_mat[1][1];
minor_mat[1][1] = temp;
temp = minor_mat[0][1];
minor_mat[0][1] = minor_mat[1][0];
minor_mat[1][0] = temp;
}
return minor_mat;
}
//the same problem occurs here as it did in the minor method
//m appears to get modified even though I only use m.clone()
public float[][] cofactor(float [][] m)
{
float[][] res = m.clone();
res = this.minor(res)
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
res[i][j] = res[i][j]*(int)Math.pow(-1, i + j);
}
}
return res;
}
//The following transpose, adjugate, and inverse methods have the same problem
public float[][] transpose(float[][] m)
{
float[][] res = new float[m.length][m.length];
float temp = 0;
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
temp = m[i][j];
res[i][j] = m[j][i];
res[j][i] = temp;
}
}
return res;
}
public float[][] adjugate(float[][] m)
{
float[][] res = this.transpose(this.cofactor(m));
return res;
}
public float[][] inverse(float[][] m)
{
float[][] res = this.mul(this.adjugate(m), (1/this.determinant(m)));
return res;
}
//print out the matrix in square form
public void matrixprint(float [][] m)
{
for(int i = 0; i < m.length; i++)
{
System.out.println("");
for(int j = 0; j < m[i].length; j++){
System.out.print(m[i][j] + " ");
}
}
System.out.println("\n");
}
}
Now the main class and the main method that creates an instance of the MatrixOperations class and uses its methods on a 2X2 matrix.
public class Main {
public static void main(String[] args) {
MatrixOperations mo = new MatrixOperations();
//Create a 2X2 matrix called "matrix" and set its elements
//Then perform each step on "matrix" and finally test if you have acquired the correct inverse
float [][] matrix = new float[2][2];
matrix[0][0] = 2;
matrix [0][1] = 5;
matrix [1][0] = 4;
matrix [1][1] = 3;
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Minor = ");
mo.matrixprint(mo.minor(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Cofactor = ");
mo.matrixprint(mo.cofactor(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Adjugate = ");
mo.matrixprint(mo.adjugate(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Determinant = ");
System.out.println(mo.determinant(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Inverse = ");
mo.matrixprint(mo.inverse(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Identity = ");
mo.matrixprint(mo.mul(mo.inverse(matrix), matrix));
}
}
Now you will see that when I show the output, every time I use a method on "matrix", and reprint "matrix", "matrix" itself has been modified even though my methods only use a copy of "matrix" and not "matrix" itself.
Output:
Matrix =
2.0 5.0
4.0 3.0
Minor =
3.0 4.0
5.0 2.0
Matrix =
3.0 4.0
5.0 2.0
Cofactor =
3.0 -4.0
-5.0 2.0
Matrix =
3.0 -4.0
-5.0 2.0
Adjugate =
3.0 5.0
4.0 2.0
Matrix =
3.0 4.0
5.0 2.0
Determinant =
-14.0
Matrix =
3.0 4.0
5.0 2.0
Inverse =
-0.21428573 0.35714287
0.2857143 -0.14285715
Matrix =
3.0 -4.0
-5.0 2.0
Identity =
0.1479592 0.1479592
0.12244898 0.12244898
Any help/explanation as to why this happens would be appreciated.
This line does a shallow clone;
float[][] res = m.clone();
This copies the res which is array fo references to arrays. but not any of the arrays res points to. Most likely what you wanted is
float[][] res = new float[m.length][];
for (int i = 0; i < m.length; i++)
res[i] = m[i].clone();
It's because you are passing reference of matrix object in the methods of MatrixOperations class. It's not a copy of matrix object.
From Java doc:
Reference data type parameters, such as objects, are also passed into
methods by value. This means that when the method returns, the
passed-in reference still references the same object as before.
A two-dimensional array is just an array of arrays.
clone() on an array just does a shallow clone.
So you have a new cloned outer array, but it references the same entries (the inner arrays).
After cloning the outer array, iterate over the outer array and clone all inner arrays to get a deep clone.
I am trying to solve the following linear problem by using the Simplex solver from apache-commons: org.apache.commons.math3.optim.linear.SimplexSolver.
n is the number of rows
m is the number of columns
L is a global limit for the sum value of each row
This is what I have so far:
List<LinearConstraint> constraints = new ArrayList<>();
double[][] A = calculateAValues();
// m = count of columns
// constraint 1: the sum of values in all column must be <= 1
for(int i = 0; i < m; i++) {
double[] v = new double[n];
for(int j=0; j < n; j++) {
v[j] = 1;
}
constraints.add(new LinearConstraint(v, Relationship.LEQ, 1));
}
// n = count of rows
// constraint 2: sum of a_i,j in all row must be <= L (Limit)
for(int i = 0; i < n; i++) {
double[] v = new double[m];
for(int j=0; j < m; j++) {
v[j] = A[i][j];
}
constraints.add(new LinearConstraint(v, Relationship.LEQ, L));
}
double[] objectiveCoefficients = new double[n * m];
for(int i = 0; i < n * m; ++i) {
objectiveCoefficients[i] = 1;
}
LinearObjectiveFunction objective = new LinearObjectiveFunction(objectiveCoefficients, 0);
LinearConstraintSet constraintSet = new LinearConstraintSet(constraints);
SimplexSolver solver = new SimplexSolver();
PointValuePair solution = solver.optimize(objective, constraintSet, GoalType.MAXIMIZE);
return solution.getValue();
I have trouble getting the objective function right, and also it might be some other things missing. My every attempt so far resulted in UnboundedSolutionException.
The error seems to be in the coefficient arrays of the linear constraints.
You have n*m variables therefore the coefficient arrays for the constraints and the objective functions must have length n*m. Unfortunately the SimplexSolver silently expands the constraints array if they are shorter than the array of the objective function. So your code did not specify the correct constraints leading to an unbounded solution.
Constraint 1: the sum of values in all column must be <= 1
for(int j=0; j<m; j++)
{
double[] v = new double[n*m];
for(int i=0; i<n; i++)
v[i*n + j] = 1;
constraints.add(new LinearConstraint(v, Relationship.LEQ, 1));
}
Constraint 2: sum of a_i,j in all row must be <= L (Limit)
// n = count of rows
for(int i=0; i<n; i++)
{
double[] v = new double[n*m];
for(int j=0; j<m; j++)
v[i*n + j] = A[i][j];
constraints.add(new LinearConstraint(v, Relationship.LEQ, L));
}
Objective coffecifients:
double[] objectiveCoefficients = new double[n * m];
Arrays.fill(objectiveCoefficients, 1.0);
LinearObjectiveFunction objective = LinearObjectiveFunction(objectiveCoefficients, 0);
The constraint x_ij <= 1 is already fulfilled because of constraint 2.
Maybe it makes things more clear to also explicitly specify a constraint for 0 <= x_ij using a NonNegativeConstraint:
SimplexSolver solver = new SimplexSolver();
PointValuePair solution = solver.optimize(objective, constraintSet,
GoalType.MAXIMIZE, new NonNegativeConstraint(true));
hi I'm having a little problem with arrays.
here's the code:
int frame_size = 410;
int frame_shift = 320;
ArrayList<double[]> frames = new ArrayList<double[]>();
for (int i = 0; i + frame_size < inbuf.length; i = i + frame_shift) {
double[] frame = new double[frame_size];
System.arraycopy(inbuf, i, frame, 0, frame_size);
frames.add(frame);
}
here I share a large array into several small, and add them to arraylist
I need to get more of ArrayList arrays and pass them to the function, and then accept the answer and assemble arrays processed one:
int[] Cover = new int[frames.size() * nParam];
for (int i = 0; i < frames.size(); i++) {
double[] finMc = Gos.getVek(frames.get(i));
for (int c = 0; c < finMc.length; c++) {
int mc = (int) finMc[c];
for (int m = 0; m < Cover.length; m++) {
Cover[m] = mc;
}
}
}
all this code does not work (
all elements of the array are zero Cover.
Сover[0] = 0
Cover[1] = 0
Cover[2] = 0
...
help solve the problem, please!)
thank you in advance)
Update
int frame_size = 410;
int frame_shift = 320;
ArrayList<double[]> frames = new ArrayList<double[]>();
for (int i = 0; i + frame_size < inbuf.length; i = i + frame_shift) {
double[] frame = new double[frame_size];
System.arraycopy(inbuf, i, frame, 0, frame_size);
frames.add(frame);
}
int[] Cover = new int[frames.size() * nParam];
for (int i = 0; i < frames.size(); i++) {
double[] finMc = Gos.getVek(frames.get(i));
for (int c = 0; c < finMc.length; c++) {
int mc = (int) finMc[c];
Cover[i * frames.size() + c] = (int) finMc[c];
}
}
Code^ not work(
UPDATE 2
double[] inbuf = new double[Size];
inbuf = toDoubleArray(Gos.data);
inbuf[2] = 10;
inbuf[4] = 14;
toDoubleArray
public static double[] toDoubleArray(byte[] byteArray) {
int times = Double.SIZE / Byte.SIZE;
double[] doubles = new double[byteArray.length / times];
for (int i = 0; i < doubles.length; i++) {
doubles[i] = ByteBuffer.wrap(byteArray, i * times, times)
.getDouble();
}
return doubles;
}
Code not work:
int frame_size = 410;
int frame_shift = 320;
ArrayList<double[]> frames = new ArrayList<double[]>();
for (int i = 0; i + frame_size < inbuf.length; i = i + frame_shift) {
double[] frame = new double[frame_size];
System.arraycopy(inbuf, i, frame, 0, frame_size);
frames.add(frame);
}
double[] Cover = new double[frames.size() * nParam];
for (int i = 0; i < frames.size(); i++) {
double[] finMc = Gos.getVek(frames.get(i));
for (int c = 0; c < finMc.length; c++) {
Cover[i * frames.size() + c] = finMc[c];
}
}
A couple of thoughts spring to mind immediately:
1)
for (int m = 0; m < Cover.length; m++) {
Cover[m] = mc;
}
This block starts m over at 0 every time through the loop. This means you're always writing over the same portion of the Cover array. So effectively, it's only the last frame's data that's stored. You probably meant
for(int m = i * frames.size(); m < (i+1)*frames.size(); i++) {
Cover[m] = mc;
}
But this raises a further issue -- you're writing the same value (mc) into the entire area allocated for a whole frame of data. You probably want to merge this loop with the previous loop so that this doesn't happen.
for (int c = 0; c < finMc.length; c++) {
Cover[i * frames.size() + c] = (int)finMc[c];
}
2) int mc = (int) finMc[c];
That line casts the value to an int which truncates the value stored at finMc[c]. If finMc[c] is between 0 and 1 this will yield 0 when the data is copied and casted. This is compounded by the previous issue which ensures that only the last frame's data ever gets copied. This is simply solved by removing the cast and declaring Cover as an array of doubles instead of ints.
So in sum, the code might work a bit better if it's written this way:
double[] Cover = new double[frames.size() * nParam];
for (int i = 0; i < frames.size(); i++) {
double[] finMc = Gos.getVek(frames.get(i));
for (int c = 0; c < finMc.length; c++) {
Cover[i * frames.size() + c] = finMc[c];
}
}