//Objective function
double[] c = new double[] { -1., -1. };
//Inequalities constraints
double[][] G = new double[][] {{4./3., -1}, {-1./2., 1.}, {-2., -1.}, {1./3., 1.}};
double[] h = new double[] {2., 1./2., 2., 1./2.};
//Bounds on variables
double[] lb = new double[] {0 , 0};
double[] ub = new double[] {10, 10};
//optimization problem
LPOptimizationRequest or = new LPOptimizationRequest();
or.setC(c);
or.setG(G);
or.setH(h);
or.setLb(lb);
or.setUb(ub);
or.setDumpProblem(true);
//optimization
LPPrimalDualMethod opt = new LPPrimalDualMethod();
opt.setLPOptimizationRequest(or);
opt.optimize();
These code are copied from JOptimizer official document. However, it only shows how to use inequality constraints in linear programming in JOptimizer. I am wondering how to represent an equality constraints in JOptimizer? I know I can achieve this goal by defining multiple inequality constraints, say Ax < b and -Ax < -b are the same thing as Ax=b. But is there any direct way for me to do this?
Okay, I've found the answer. It's quite easy. We just need to replace setG and setH with setA and setB.
Related
For leetcode problem https://leetcode.com/problems/walls-and-gates/solution/
The BFS solution has
.......
private static final List<int[]> DIRECTIONS = Arrays.asList(
new int[] { 1, 0},
new int[] {-1, 0},
new int[] { 0, 1},
new int[] { 0, -1}
);
.....
This is to take turn right while traversing the matrix. Does anyone know how to derive these vectors without memorizing them?
My understanding is you do a cross product with (x,y,0) X (0,0,1) which gives R = yX -xY + 0Z.
I think you take a partial derivative here but I'm unsure of the math proof which gives the directions.
Please explain mathematically how to get vectors {1,0} , {-1, 0}, {0,1} , {0,-1}
My understanding is you do a cross product with (x,y,0) X (0,0,1) which gives R = yX -xY + 0Z.
I think you take a partial derivative here but I'm unsure of the math proof which gives the directions.
Are there any built-in functions (or fast implementations) in Java to convert a single-dimension array of size [n] to a two-dimensional array of size [n][1]?
For example:
double[] x = new double[]{1, 2, 3};
double[][] x2D = new double[][]{{1}, {2}, {3}};
Arrays.setAll method can be used:
double[] x = {1, 2, 3};
double[][] x2D = new double[x.length][];
Arrays.setAll(x2D, i -> new double[]{x[i]});
If you're looking for a built-in function to convert a single dimension array to a two-dimensional one, sadly there isn't one.
However, as it has been pointed out in the comments, you could use streams to reach a fair compact way to initialize your two-dimensional array from a single-dimension one. Basically, you could define your matrix's dimension from the array's length and then rely on a stream to copy the array's elements within the two-dimensional array.
double[] x = new double[]{1, 2, 3};
double[][] x2D = new double[x.length][1];
IntStream.range(0, x.length).forEach(i -> x2D[i][0] = x[i]);
Or in a more concise writing, you could immediately initialize your two-dimensional array with a stream where each element is mapped to a double[] and then collect each one of them into a further array.
double[] x = new double[]{1, 2, 3};
double[][] mat = Arrays.stream(x).mapToObj(i -> new double[]{i}).toArray(double[][]::new);
I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
Check out the Arrays.fill methods.
int[] array = new int[4];
Arrays.fill(array, 1); // [1, 1, 1, 1]
You can also do it as part of the declaration:
int[] a = new int[] {0, 0, 0, 0};
Arrays.fill(). The method is overloaded for different data types, and there is even a variation that fills only a specified range of indices.
In Java-8 you can use IntStream to produce a stream of numbers that you want to repeat, and then convert it to array. This approach produces an expression suitable for use in an initializer:
int[] data = IntStream.generate(() -> value).limit(size).toArray();
Above, size and value are expressions that produce the number of items that you want tot repeat and the value being repeated.
Demo.
Arrays.fill(arrayName,value);
in java
int arrnum[] ={5,6,9,2,10};
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Arrays.fill(arrnum,0);
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Output
5 6 9 2 10
0 0 0 0 0
An array can be initialized by using the new Object {} syntax.
For example, an array of String can be declared by either:
String[] s = new String[] {"One", "Two", "Three"};
String[] s2 = {"One", "Two", "Three"};
Primitives can also be similarly initialized either by:
int[] i = new int[] {1, 2, 3};
int[] i2 = {1, 2, 3};
Or an array of some Object:
Point[] p = new Point[] {new Point(1, 1), new Point(2, 2)};
All the details about arrays in Java is written out in Chapter 10: Arrays in The Java Language Specifications, Third Edition.
Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false.
To fill the array with something else you can use Arrays.fill() or as part of the declaration
int[] a = new int[] {0, 0, 0, 0};
There are no shortcuts in Java to fill arrays with arithmetic series as in some scripting languages.
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
Check out the Arrays.fill methods.
int[] array = new int[4];
Arrays.fill(array, 1); // [1, 1, 1, 1]
You can also do it as part of the declaration:
int[] a = new int[] {0, 0, 0, 0};
Arrays.fill(). The method is overloaded for different data types, and there is even a variation that fills only a specified range of indices.
In Java-8 you can use IntStream to produce a stream of numbers that you want to repeat, and then convert it to array. This approach produces an expression suitable for use in an initializer:
int[] data = IntStream.generate(() -> value).limit(size).toArray();
Above, size and value are expressions that produce the number of items that you want tot repeat and the value being repeated.
Demo.
Arrays.fill(arrayName,value);
in java
int arrnum[] ={5,6,9,2,10};
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Arrays.fill(arrnum,0);
for(int i=0;i<arrnum.length;i++){
System.out.println(arrnum[i]+" ");
}
Output
5 6 9 2 10
0 0 0 0 0
An array can be initialized by using the new Object {} syntax.
For example, an array of String can be declared by either:
String[] s = new String[] {"One", "Two", "Three"};
String[] s2 = {"One", "Two", "Three"};
Primitives can also be similarly initialized either by:
int[] i = new int[] {1, 2, 3};
int[] i2 = {1, 2, 3};
Or an array of some Object:
Point[] p = new Point[] {new Point(1, 1), new Point(2, 2)};
All the details about arrays in Java is written out in Chapter 10: Arrays in The Java Language Specifications, Third Edition.
Array elements in Java are initialized to default values when created. For numbers this means they are initialized to 0, for references they are null and for booleans they are false.
To fill the array with something else you can use Arrays.fill() or as part of the declaration
int[] a = new int[] {0, 0, 0, 0};
There are no shortcuts in Java to fill arrays with arithmetic series as in some scripting languages.
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
please I am trying to understand matrix computation.
and my question may seem simple but please need an answer
can some briefly explain to me what is an RHS vector.
I often see it used in the Apache commons math library
for example i got this from a stackoverflow page:
public class LinearAlgebraDemo
{
public static void main(String[] args)
{
double [][] values = {{1, 1, 2}, {2, 4, -3}, {3, 6, -5}};
double [] rhs = { 9, 1, 0 }; /* RHS Vector */
RealMatrix a = new Array2DRowRealMatrix(values);
DecompositionSolver solver = new LUDecompositionImpl(a).getSolver();
RealVector b = new ArrayRealVector(rhs);
RealVector x = solver.solve(b);
RealVector residual = a.operate(x).subtract(b);
double rnorm = residual.getLInfNorm();
}
}
can someone explain this code to me and especially the RHS vector and its purpose.
thank you very much.
You matrix equation looks like this:
Ax = b
where A is a matrix with m rows and n columns, x is a column vector of m unknowns, and b is another column vector (aka The Right-Hand Side) of m known values. It's on the right hand side of the equals sign - hence the name.
If I gave you a simple equation with two numbers and an unknown value x, you'd know exactly how to solve it:
Ax = b -> x = b/A
Think of this as solving for x by multiplying both sides of the equation by the inverse of A.
In this case it's more complicated, because dividing by a matrix means inverting it.
You're not going to invert the matrix; you're going to create something called an LU decomposition of the matrix A. You should read about what that is and why it's better than calculating a full inverse if you're interested.
RHS is a common mathematical abbreviation for "right hand side". Here it appears that you are solving a system of linear equations Ax = b where A is an n x n matrix and x and b are n-dimensional column vectors. If you don't understand this terminology, then I suggest you study up on linear algebra.
As for the code, rhs is an array which is used to initialize the elements of the vector b. Similarly, the 2D array values is used to initialize the elements of the matrix A (actually a reference variable named a).