Summing over a N dimensional function - Monte Carlo Integration? - java

I wanted to evaluate a certain sum needed for a monte carlo method in N dimensions. In N dimensions I have N variables with M data points, meaning that I basically have M N-D vectors representing M sampling points. I have to hold one variable fixed while I sum over all other variables for M values. Currently my code is (assuming my mesh which has N columns and M rows is static):
public static double myFunction(int row, int col){
double checkMesh = mesh[row][col];
return myFunctionHelper(0,col,checkMesh);
}
public static double myFunctionHelper(int j, int col, double checkMesh){
//function is initialized with j=0
if (j >= mesh[0].length){
return 0;
}
double sum = 0;
for (int i=0 ; i<mesh.length ; i++){
sum += myFunctionHelper(j+1,col,checkMesh);
if (j==mesh[0].length-1){
if (mesh[0][col]==checkMesh){
sum += function(mesh[0]);
}
mesh = rotate(mesh,j);
}
}
if (j>=1){
mesh = rotate(mesh,j-1);
}
return sum;
}
Now this function works but it doesn't hold any variables fixed, instead it just sums over all possible combinations of variables. I was wondering if anyone had any suggestions on how I can alter this to make it work as I want it to. Currently the only workaround that I can think of is to remove the fixed column from my matrix, sum over the combinations of the altered matrix, and when the function gets evaluated make sure the fixed variable is included in the right spot of an altered list, except I want to avoid doing this as it requires more memory and isn't as clean. I'd appreciate the help :).
EDIT: The rotate function takes the mesh and rotates the j-th column by one vertically upwards movement. Also function takes a vector as an argument, where the vector is represented by an array.

(1) You are computing the sum recursively, through myFunctionHelper calling myFunctionHelper. This may work, but it makes the code more involved than necessary. Recursion is not really not suited for this kind of numeric computation. Rewrite your algorithm without recursion, as a double loop over row and columnd indices, and things will get much clearer.
(2) With mesh[0][col]==checkMesh you try to identify the fixed entry by value. Why not by column and row index?

Related

Calculate absolute minimum difference between any two numbers of a huge integer array

I have a very long array in a Java program (300 000+ unsorted integers) and need to calculate the minimum absolute difference between any two numbers inside the array, and display the absolute difference and the corresponding pair of numbers as an output. The whole calculation should happen very quickly.
I have the following code, which would usually work:
private static void calcMinAbsDiff(int[] inputArray)
{
Arrays.sort(inputArray);
int minimum = Math.abs(inputArray[1] - inputArray[0]);
int firstElement = inputArray[0];
int secondElement = inputArray[1];
for (int i = 2; i < inputArray.length; i++)
{
if(Math.abs(inputArray[i] - inputArray[i-1]) < minimum)
{
minimum = Math.abs(inputArray[i] - inputArray[i-1]);
firstElement = inputArray[i-1];
secondElement = inputArray[i];
}
}
System.out.println("Minimum Absolute Difference : "+minimum);
System.out.println("Pair of Elements : ("+firstElement+", "+secondElement+")");
}
However, the output I receive is all 0s. I believe this is because the array is way too long.
If you have two or more zeros and no negative integers in your dataset, then your output is expected. After sorting, then inputArray[0] and inputArray[1] would both be 0, and the difference would be 0. No other pair of adjacent elements would have an absolute difference less than 0, so minimum, firstElement, and second Element would all be 0 at the end of the algorithm.
If you really have no zeros in your dataset, or if you do have negative integers, then you may have an initialization problem. Check this thread:
Why is my simple Array only printing zeros in java?
If that's not it, then only other thing I can think of is that you have a problem in the previous scope causing the data to get zeroed out.
I would try printing samples of your dataset at various points to see exactly where/when it's getting zeroed.
If you still have trouble, then post more info on the dataset and the scope which calls this function to help us see what's going on. Let us know how you make out!

How to convert Pascal Triangle Generator from 2-D to 1-D arrays?

I have a question.
I've made a Pascal Triangle Generator program in Java using 2-D arrays and it was generally simple.
However, I can't seem to figure out how to program the same thing using only a 1-D array.
Does anyone have any suggestions? I will try to insert code once when I can, but I'm asking this in a general sense; what should you think about when converting 2-D arrays to 1-D array?
Thanks.
All the answers posted thus far have given cookie-cutter solutions to turning a 2D array into a 1D array. As far as I can tell, storing a Pascal's triangle in a statically dimensioned 2D array is pretty inefficient since around half the entries are unused.
The main issue is that the triangle is ... well, triangular, whereas a rectangular 2D matrix is relatively easy to unroll.
However, a triangle is still quite simple to unroll. You just need to figure out where each offset is. Consider storing your pascal's triangle as follows in a 1-D array:
1,1,1,1,2,1,1,3,3,1
Consider the ith row in the Pascal's triangle. Its first entry is in the (1+2+...+i-1)th entry in the 1-dimensional array. This is a simple arithmetic progression sum which evaluates to (i-1)(i)/2. So, write some functions like:
int getArrayOffset(int row, int offset) {
// assert(row>0);
return (row*(row-1))/2 + offset - 1;
}
int calculateEntry(int row, int offset, int[] triangle) {
triangle[getArrayOffset(row,offset)] = triangle[getArrayOffset(row-1,offset-1)]+triangle[getArrayOffset(row-1,offset)];
}
void calculatePascal(int n) {
int [] triangle = new int[getArrayOffset(n+1,1)];
for (int row=1; row <=n; row++) {
triangle[getArrayOffset(row,1)]=1;
triangle[getArrayOffset(row,row)]=1;
for (int offset=2; offset < row; offset++) {
calculateEntry(row,offset,triangle);
}
}
}
You just have to replace all accesse operation to your array from data[x][y] to data[x+y*width] or data[x*height+y].
If you have only given the index of a field and you want to calculate the coordinates that would be
x=index%width;
y=index/width; //be sure to use integer division here
or
x=index/width; //be sure to use integer division here
y=index%width;
You can find out more about pairing functions here:
http://en.wikipedia.org/wiki/Cantor_pairing_function
I am not sure until I see your code, but generally -
you can try different addressing mode in your code.
2D case: array is mxn... Addressing goes [i,j]
same data can be stored in 1D, row after another / column wise. Lets say it was row wise. Then the addressing mode will be -[i*(n) + j]
where i=0..m & j=0..n
Cheers,
Rishikesh

multivariate non-linear optimisation library for java similar to Matlab's solver GRG algorithm

I have been looking for a good optimization algorithm for almost a year now.
My problem consists of taking a matrix of observed values, lets call it 'M' and using a function 'F' which by transforming each of M's cells, one-by-one, produces another Matrix 'N'.
Then matrices 'M' and 'N' are compared using least square method and the distance between them should be minimized by changing the variables of 'F'.
There is an array of variables lets call it 'a' and a single variable 'b' which are used in the function F.
The variable 'b' is consistent between all of the calculations required to get the matrix 'N'.
The length of array 'a' depends on the number of rows; one number from array 'a' corresponds to each row.
So lets say to calculate the 3rd row of 'N' I use F on the value of each cell in the 3rd row of 'M' together with the variables a[3] and b.
To calculate the 4th row of N I calculate F with the value of each cell from the 4th row in M in turn together with a[4] and b.
And so on, and so on.
Once I calculate the whole of N, I need to compare it to M and minimize their distance by adjusting the array of variables a[] and the variable b.
I have been using Apache cmaes for smaller matrices but it doesn't work as well as matlab's solver on large matrices
EDIT
So ill try to describe this algorithmically as opposed to mathematically as that is my stronger side.
double[w,h] m //Matrix M
double[w,h] n //Matrix N
double[] hv // this is an array of constant hardcoded values
double[] a // this array is initialised to an initial guess
double b //also initialised to an initial guess
double total //target value, this value needs to be minimised
//w and h are constant
for(i=0; i<h; i++){
for(j=0; j<w; j++)
m[i,j] = getObservedValue[i,j] //observed values are not under my control
}
}
for(i=0; i<h; i++){
for(j=0; j<w; j++)
n[i,j] = 0.75/1+e^(-b*(hv[i]-a[i]))+25
}
}
//once N is calculated initially using guesses for a[] and b
for(i=0; i<h; i++){
for(j=0; j<w; j++)
total = total + (m[i,j]*(m[i,j]-n[i,j])^2) //sum of square distances
}
}
Now the objective is to minimise 'total'(distance between M and N) by finding the optimum values for a[] and b.
Perhaps if someone has done something similar they could point me to a library?
Or a quick demo of how i could find the optimal values my self?
Thanks very much for reading this,
Erik

Creating an even amount of randomness in an array

Let's say that you have an arbitrarily large sized two-dimensional array with an even amount of items in it. Let's also assume for clarity that you can only choose between two things to put as a given item in the array. How would you go about putting a random choice at a given index in the array but once the array is filled you have an even split among the two choices?
If there are any answers with code, Java is preferred but other languages are fine as well.
You could basically think about it in the opposite way. Rather than deciding for a given index, which value to put in it, you could select n/2 elements from the array and place the first value in them. Then place the 2nd value in the other n/2.
A 2-D A[M,N] array can be mapped to a vector V[M*N] (you can use a row-major or a column-major order to do the mapping).
Start with a vector V[M*N]. Fill its first half with the first choice, and the second half of the array with the second choice object. Run a Fisher-Yates shuffle, and convert the shuffled array to a 2-D array. The array is now filled with elements that are evenly split among the two choices, and the choices at each particular index are random.
The below creates a List<T> the size of the area of the matrix, and fills it half with the first choice (spaces[0]) and half with the second (spaces[1]). Afterward, it applies a shuffle (namely Fisher-Yates, via Collections.shuffle) and begins to fill the matrix with these values.
static <T> void fill(final T[][] matrix, final T... space) {
final int w = matrix.length;
final int h = matrix[0].length;
final int area = w * h;
final List<T> sample = new ArrayList<T>(area);
final int half = area >> 1;
sample.addAll(Collections.nCopies(half, space[0]));
sample.addAll(Collections.nCopies(half, space[1]));
Collections.shuffle(sample);
final Iterator<T> cursor = sample.iterator();
for (int x = w - 1; x >= 0; --x) {
final T[] column = matrix[x];
for (int y = h - 1; y >= 0; --y) {
column[y] = cursor.next();
}
}
}
Pseudo-code:
int trues_remaining = size / 2;
int falses_remaining = size / 2;
while (trues_remaining + falses_remaining > 0)
{
if (trues_remaining > 0)
{
if (falses_remaining > 0)
array.push(getRandomBool());
else
array.push(true);
}
else
array.push(false);
}
Doesn't really scale to more than two values, though. How about:
assoc_array = { 1 = 4, 2 = 4, 3 = 4, 4 = 4 };
while (! assoc_array.isEmpty())
{
int index = rand(assoc_array.getNumberOfKeys());
int n = assoc_array.getKeyAtIndex(index);
array.push(n);
assoc_array[n]--;
if (assoc_array[n] <= 0) assoc_array.deleteKey(n);
}
EDIT: just noticed you asked for a two-dimensional array. Well it should be easy to adapt this approach to n-dimensional.
EDIT2: from your comment above, "school yard pick" is a great name for this.
It doesn't sound like your requirements for randomness are very strict, but I thought I'd contribute some more thoughts for anyone who may benefit from them.
You're basically asking for a pseudorandom binary sequence, and the most popular one I know of is the maximum length sequence. This uses a register of n bits along with a linear feedback shift register to define a periodic series of 1's and 0's that has a perfectly flat frequency spectrum. At least it is perfectly flat within certain bounds, determined by the sequence's period (2^n-1 bits).
What does that mean? Basically it means that the sequence is guaranteed to be maximally random across all shifts (and therefore frequencies) if its full length is used. When compared to an equal length sequence of numbers generated from a random number generator, it will contain MORE randomness per length than your typical randomly generated sequence.
It is for this reason that it is used to determine impulse functions in white noise analysis of systems, especially when experiment time is valuable and higher order cross effects are less important. Because the sequence is random relative to all shifts of itself, its auto-correlation is a perfect delta function (aside from qualifiers indicated above) so the stimulus does not contaminate the cross correlation between stimulus and response.
I don't really know what your application for this matrix is, but if it simply needs to "appear" random then this would do that very effectively. In terms of being balanced, 1's vs 0's, the sequence is guaranteed to have exactly one more 1 than 0. Therefore if you're trying to create a grid of 2^n, you would be guaranteed to get the correct result by tacking a 0 onto the end.
So an m-sequence is more random than anything you'll generate using a random number generator and it has a defined number of 0's and 1's. However, it doesn't allow for unqualified generation of 2d matrices of arbitrary size - only those where the total number of elements in the grid is a power of 2.

Transpose matrix stored in a 1-dimensional array without using extra memory [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In-place transposition of a matrix
Recently attended an Technical Written Interview. Came through the following question.
I have an array as say
testArray = {a1,a2,a3,...an,b1,b2,b3,....bn,c1,c2,c3,.....,cn}
I need to sort this array as `
testArray = {a1,b1,c1,a2,b2,c2,a3,b3,c3,.....,an,bn,cn}
Constraint is I should not use extra memory, should not use any inbuilt function.
Should write complete code, it can be in any language and can also use any data structure.
eg:
Input: {1,2,3,4,5,6,7,8,9}, n = 3
Output: {1,4,7,2,5,8,3,6,9}
I could not get any solution within the constraint, can anyone provide solution or suggestion?
This is just a matrix transpose operation. And there is even a problem and solution for in-place matrix transposition on Wikipedia.
No extra space is impossible, since you need to at least go through the array. O(1) additional memory is possible, with heavy penalty on the time complexity.
The solution is built on follow-the-cycle algorithm in the Wikipedia page: for each cell, we will find the cell with the smallest index in the cycle. If the cell with the smallest index is greater than or equal (>=) to the index of the current cell, we will perform chain swapping. Otherwise, we ignore the cell, since it has been swapped correctly. The (loosely analyzed) upper bound on time complexity can go as high as O((MN)2) (we go through M * N cells, and the cycle can only be as long as the total number of cells).
Impossibility
It is impossible to implement this algorithm without extra use of memory and an arbitrary length because you need a an iterator to traverse the list and that takes up space.
Finding the right indices to swap
For fixed lengths of the array and fixed n you can use a matrix transpose algorithm.
and in order to swap the elements y
The algorithm you are looking for is a matrix transpose algorithm.
so you have to swap every element exactly once iterating through it.
http://en.wikipedia.org/wiki/Transpose
basically you have to swap the m -th element in the n - th component with the n - th element in the m -th component. This can be done by a double loop.
m = length(array)/n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
{
index_1 = i * m + j;
index_2 = j * m + i
swap(index_1, index_2);
}
Note: For fixed m and n this loop can be completely unrolled and therefore m, i, j can be replaced by a constant.
Swaping without Memory consumption
In order to swap every element without using extra space you can use the XOR swap algorithm as pointed out in the comments:
X := X XOR Y
Y := Y XOR X
X := X XOR Y
The simplest way to swap two numbers (a and b) without using a temporary variable is like this:
b = b + a;
a = b - a;
b = b - a;
If you write that in a function, then you're part of the way there. How you keep track of which variable to swap within the arrays without using a temporary variable eludes me right now.
Bear in mind voters: he doesn't actually need to sort the array, just swap the right values.
Edit: this will work with large values in Java (and in C/C++ unless you turn on some very aggressive compiler optimisations - the behaviour is undefined but defaults to sane). The values will just wrap around.
Second edit - some (rather untested) code to flip the array around, with I think 4 integers over the memory limit. It's while technically massively unthreadsafe, but it would be parallelisable just because you only access each array location once at most:
static int[] a = {1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16};
static int n = 4;
public static void main(String[] args)
{
for(int i = 0; i < a.length/n; i++) // 1 integer
for(int j = 0; j < n; j++) // 1 integer
if(j > i)
swap(i*n+j, j*n+i);
}
static void swap(int aPos, int bPos) // 2 integers
{
if(a[aPos] != a[bPos])
{
a[bPos] = a[aPos] + a[bPos];
a[aPos] = a[bPos] - a[aPos];
a[bPos] = a[bPos] - a[aPos];
}
}
Apologies if this misunderstands the question; I read it carefully and couldn't work out what was needed other than this.
Take a look at Quicksort algorithm
For more information about available algorithms, go to Sorting algorithm page.

Categories