I have the following very simple code that is supposed to iteratively change the values of a matrix (finalvals)until the row sums and column sums approach certain values (given by b1 and c) -
double[] rowsums = new double[3];
double[] colsums = new double[3];
Double[][] finalvals = {
{10320289.32d,15531663.71d,513718885.9d},
{5741307.806d,19279894.22d,254573082.9d},
{216919827.7d,229857986.8d,8769234962d}
};
Double[] b1 = {544169638d,273919997d,9217088452d};
Double[] c = {232981430d,264669549d,9537527108d};
for(int k = 0;k<1000;k++){
for(int i = 0;i<3;i++){
for(int j = 0;j<3;j++){
rowsums[i] = rowsums[i] + finalvals[i][j];
}
}
for(int i = 0;i<3;i++) {
for(int j = 0;j<3;j++) {
finalvals[i][j] = b1[i] * finalvals[i][j] / rowsums[i];
}
}
for(int i = 0;i<3;i++) {
for(int j = 0;j<3;j++) {
colsums[j] = colsums[j] + finalvals[i][j];
}
}
for(int i = 0;i<3;i++) {
for(int j = 0;j<3;j++) {
finalvals[i][j] = c[j] * finalvals[i][j] / colsums[j];
}
}
}
for(int i = 0;i<3;i++) {
for(int j = 0;j<3;j++) {
System.out.print(finalvals[i][j] + " ");
}
System.out.print("\n");
}
However, the due to numerical leaks, the values of finalvals just become all zeros after a thousand iterations. Is there any way to plug these leaks?
Edit: A description of the algorithm - we want the matrix rows to sum to the arrays b1 and matrix columns to sum to array c. So, first distribute the first value of b1 among the first three rows of the matrix in proportion of the existing values and similarly for the other two rows. Then we do the same using the columns and the array c. We do this iteratively many times and we should finally get a matrix whose rows and columns sum appropriately.
If your algorithm gives excessive rounding errors, the solution should be to find a better algorithm, rather than to use higher precision numerics. This looks like a classic linear programming or simultaneous equation problem, for which there are tried and tested algorithms that work well. You need to sit down and study a bit on numerical methods.
Ok, I figured it out. The reason was actually not rounding errors at all, but my code was wrong. I should have been setting the rowsums and colsums to zero after every iteration of the k loop. Thanks for every ones help.
Related
I came across this problem in class and I'm stuck on it. I did plenty of research but I'm not being able to fix my code.
I need to create a matrix and find the smallest value in the row of the largest value (I believe this element is called minimax). I'm trying to do with a simple 3 x 3 matrix. What I have so far:
Scanner val = new Scanner(System.in);
int matrizVal[][] = new int[3][3];
for (int a = 0; a < matrizVal.length; a++) {
for (int b = 0; b < matrizVal.length; b++) {
System.out.print("(" + a + ", " + b + "): ");
matrizVal[a][b] = val.nextInt();
}
}
int largest = matrizVal[0][0];
int largestrow = 0;
int arr[] = new int[2];
for (int row = 0; row < matrizVal.length; row++){
for (int col = 0; col < matrizVal.length; col++){
if (largest < matrizVal[row][col]){
largest = matrizVal[row][col];
largestrow = row;
}
}
}
To find the so called minimax element I decided to create a for each loop and get all the values of largestrow except the largest one.
for (int i : matrizVal[largestrow]){
if (i != largest){
System.out.print(i);
}
}
Here's where I'm stuck! I'd simply like to 'sort' this integer and take the first value and that'd be the minimax. I'm thinking about creating an array of size [matrizVal.length - 1], but not sure if it's gonna work.
I did a lot of research on the subject but nothing seems to help. Any tips are welcome.
(I don't think it is but I apologize if it's a duplicate)
Given the code you have provided, matrizVal[largestrow] should be the row of the matrix that contains the highest valued element.
Given that your task is to extract the smallest value in this array, there are a number of options.
If you want to simply extract the minimum value, a naive approach would go similarly to how you determined the maximum value, just with one less dimension.
For example:
int min = matrizVal[largestrow][0];
for (int i = 0; i < matrizVal.length; i++) {
if (matrizVal[largestrow][i] < min) {
min = matrizVal[largestrow][i];
}
}
// min will be the target value
Alternatively, if you want to sort the array such that the first element of the array is always the smallest, first ensure that you're making a copy of the array so as to avoid mutating the original matrix. Then feel free to use any sorting algorithm of your choice. Arrays.sort() should probably suffice.
You can simplify your approach by scanning each row for the maximum and minimum values in that row and then deciding what to do with those values based on the maximum value found in previous rows. Something like this (untested) should work:
int largestValue = Integer.MIN_VALUE;
int smallestValue = 0; // anything, really
for (int[] row : matrizVal) {
// First find the largest and smallest value for this row
int largestRowValue = Integer.MIN_VALUE;
int smallestRowValue = Integer.MAX_VALUE;
for (int val : row) {
smallestRowValue = Math.min(smallestRowValue, val);
largestRowValue = Math.max(largestRowValue, val);
}
// now check whether we found a new highest value
if (largestRowValue > largestValue) {
largestValue = largestRowValue;
smallestValue = smallestRowValue;
}
}
This doesn't record the row index, since it didn't sound like you needed to find that. If you do, then replace the outer enhanced for loop with a loops that uses an explicit index (as with your current code) and record the index as well.
I wouldn't bother with any sorting, since that (1) destroys the order of the original data (or introduces the expense of making a copy) and (2) has higher complexity than a one-time scan through the data.
You may want to consider a different alternative using Java 8 Stream :
int[] maxRow = Arrays.stream(matrizVal).max(getCompertator()).get();
int minValue = Arrays.stream(maxRow).min().getAsInt();
where getCompertator() is defined by:
private static Comparator<? super int[]> getCompertator() {
return (a1, a2)->
Integer.compare(Arrays.stream(a1).max().getAsInt(),
Arrays.stream(a2).max().getAsInt()) ;
}
Note that it may not give you the (undefined) desired output if two rows include the same highest value .
I'm trying to implement the same idea of Natural Join in Database but on Two dimensional Arrays, so What I'm trying to do is if I have
A={[a,b],[a',b']} and B={[b,c], [b',c],[b,c']}
The result of NaturalJoin(A,B) should be:
Result = {[a,b,c], [a,b,c'],[a',b',c]}
So after I find the shared column B and compare it in both arrays, how can I combine the rows? Can you please give me some hints on how to create the joinedTableau as I don't know the number of rows from the beginning of the join, how can I create it dynamically?
This is my pseudo code:
int[][] array1;
int[][] array2;
int shared = prtiallyEqualColumn(array1,array2);
for(int i = 0; i <array1.length; i++)
{
for(int j = 0 ; j < array2.length; j++)
{
if(array1[i][shared] == array2[j][shared])
{
for(int s = 0 ; s < shared; s++)
{
joinedTableau[joinedCountRow][s] = array1[i][s];
}
for(int y=shared+1; y<array2.length;y++)
{
joinedTableau[joinedCountRow][y] = array2[j][y];
}
}
}
}
I don't know what you've done in the code as you have hidden several implementations from the code presented here in the question. I am giving you the algo :-
Each column value of array1 must be compared with each row value of
array2 to produce a natural join,only in case if they are equal, else
not.
a1 = array1.countRow();
a2 = array1.countColumn();
b1 = array2.countRow();
b2 = array2.countColumn();
i = j = 1;
while(i<=a1)
while(j<=b1)
if(array1[i][a2]==array2[j][1]) // I've made array-indices start from 1
// perform natural-join operation between array1[i]+array2[j]-common_element
// Similarly iterate the array for next passes.
If there is some mistake or something which is unclear to you,please notify me. Good luck for your code.
This yields the right answer.
is it the most efficient? I posted the results of my timing. definitely exponential growth in times
count
avg time
10
1.931190E-05
100
4.734993E-04
1000
2.540604E-02
10000
1.400114E+00
100000
9.096992E+01
#!python3
from time import time
def natural_join( S, T):
theJoin = []
for i in T:
for j in S:
if i[0] == j[1]:
theJoin.append((i[1], j[0], j[1]))
break
return theJoin
for n in range(1, 6):
A = []
B = []
for i in range(10 ** n):
A.append(('x' + str(i), 'y' + str(i)))
for i in range(10 ** n):
B.append(('y' + str(i), 'z' + str(i)))
start = time()
joined = natural_join(A ,B)
end = time()
print('{:d}, {:E}'.format(10 ** n, (end-start)/n))
I tried writing this code to test out my idea on how to calculate the average value across like indices of a hash map.
i.e. for each array contained within the hashmap if the first value for the first array was 2, and the first value for the second array was 4 , and the first value for the third array was 3, I want to assign the value of (4+3+2/3)= 3 to the final double[] array for the first index, and so on for all the indices 2 through n.
int Size = 3;
double[] AVERAGED_WEIGHTS = new double[Size];
//store weights to be averaged.
Map<Integer,double[]> cached_weights = new HashMap<Integer,double[]>();
double[] weights = new double[Size];
int iteration = 0;
do
{
weights[iteration] = Math.floor(Math.random() * 10000) / 10000;
iteration++;
//store weights for averaging
cached_weights.put( iteration , weights );
}
while (iteration < Size);
//calc averages
for (Entry<Integer, double[]> entry : cached_weights.entrySet())
{
int key = entry.getKey();
double[] value = entry.getValue();
AVERAGED_WEIGHTS[ key - 1 ] += value[ key - 1 ];
if (key == iteration)
{
AVERAGED_WEIGHTS[ key - 1 ] /= key;
}
}
for(int i = 0; i < weights.length; i++)
{
weights[i] = AVERAGED_WEIGHTS[i];
}
This mimics the structure of the original program wherein the weights are populated through a do while loop. This code is broken though and does not sucessfully perform the operation described above. I've been searching online and trying different ways to fix it but I've not been able to solve it. Perhaps someone can spot my faulty logic.
Maybe I misunderstood you, but you are not computing the average because for each array in your map, you are not taking into account all its positions. You are using the key and that makes absolutely no sense. Anyways, your code is very confusing. What you need to do is simply one loop inside the other. One going through the arrays and another going through the elements of each array. A way to compute the average is the following (in a didactic way):
//compute averages
double[] sums = new double[size];
double[] averages = new double[size];
for (Entry<Integer, double[]> entry : cachedWeights.entrySet()) {
double[] value = entry.getValue();
for(int pos=0; pos < Math.min(size, value.length); pos++){
sums[pos] += value[pos];
}
}
for(int pos=0; pos < size; pos++){
averages[pos] = sums[pos] / cachedWeights.size();
}
Sorry,this is a homework problem. I am not good with maths, so I checked out some videos to understand how two matrices are multiplied. I came up with a formula, but I do not know what I am doing wrong? This question has been answered before, but I did not understand. Thank you.
case 3:
System.out.println("THE PRODUCT OF TWO MATRICES ARE: ");
for(i =0; i< arrayList.length; i++){
for(j =0; j< arrayList1.length; j++){
for(k =0; k < arrayList1.length;k++){
multiplication = arrayList[i][k] * arrayList1[k][j] + multiplication;
}
System.out.print(arrayList[i][j]+" ");
}
System.out.println();
}
break;
First of all you should understand that the multiplication of two matrices should result in a matrice (which not appear to be the case with your multiplication variable).
I suppose you have to program the basic implementation. Let's take a look at the following matrices.
A has n rows, and m columns; said to be a matrice n x m.
Similary, B has m rows and p columns (m x p matrice). The multiplication of A x B will give you a matrice n x p.
Note that if you want to do the multiplication A x B, the matrice A must have the same number of columns that the number of rows of the matrice B.
Now each value in the matrice AB (ith row and jth column) is computed as follow:
That said, let's take a look at the Java implementation (which is a pure translation of the mathematical formula).
public static int[][] multiply(int[][] matrixA, int[][] matrixB) {
int[][] result = new int[matrixA.length][matrixB[0].length];
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
for (int k = 0; k < matrixB.length; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return result;
}
The result matrice is initialized at the right dimensions. Then the first two nested loop (with indices i and j) will loop through all the elements of elements of the resulting matrice. Then you just need the third loop to compute the sum.
You'd still need to check that the matrices you give as parameters have the correct length.
The algorithm used is pretty naive (O(n3) complexity). If you don't understand it, there's a lot of resources in the web that explains how it works; but that would more a mathematical question than a programming one.
Hope it helps ! :)
I'm trying to make an encryption program where the user enters a message and then converts the "letters into numbers".
For example the user enters a ABCD as his message. The converted number would be 1 2 3 4 and the numbers are stored into a one dimensional integer array. What I want to do is be able to put it into a 2x2 matrix with the use of two dimensional arrays.
Here's a snippet of my code:
int data[] = new int[] {10,20,30,40};
*for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for (int ctr=0; ictr<data.length(); ictr++){
a[i][j] = data[ctr];}
}
}
I know there's something wrong with the code but I am really lost.
How do I output it as the following?
10 20
30 40
(instead of just 10,20,30,40)
Here's one way of doing it. It's not the only way. Basically, for each cell in the output, you calculate the corresponding index of the initial array, then do the assignment.
int data[] = new int[] {10, 20, 30, 40, 50, 60};
int width = 3;
int height = 2;
int[][] result = new int[height][width];
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
result[i][j] = data[i * width + j];
}
}
Seems like you want to output a 2xn matrix while still having the values stored in a one-dimensional array. If that's the case then you can to this:
Assume the cardinality m of your set of values is known. Then, since you want it to be 2 rows, you calculate n=ceil(m/2), which will be the column count for your 2xn matrix. Note that if m is odd then you will only have n-1 values in your second row.
Then, for your array data (one-dimension array) which stores the values, just do
for(i=0;i<2;i++) // For each row
{
for(j=0;j<n;j++) // For each column,
// where index is baseline+j in the original one-dim array
{
System.out.print(data[i*n+j]);
}
}
But make sure you check the very last value for an odd cardinality set. Also you may want to do Integer.toString() to print the values.
Your code is close but not quite right. Specifically, your innermost loop (the one with ctr) doesn't accomplish much: it really just repeatedly sets the current a[i][j] to every value in the 1-D array, ultimately ending up with the last value in the array in every cell. Your main problem is confusion around how to work ctr into those loops.
There are two general approaches for what you are trying to do here. The general assumption I am making is that you want to pack an array of length L into an M x N 2-D array, where M x N = L exactly.
The first approach is to iterate through the 2D array, pulling the appropriate value from the 1-D array. For example (I'm using M and N for sizes below):
for (int i = 0, ctr = 0; i < M; ++ i) {
for (int j = 0; j < N; ++ j, ++ ctr) {
a[i][j] = data[ctr];
}
} // The final value of ctr would be L, since L = M * N.
Here, we use i and j as the 2-D indices, and start ctr at 0 and just increment it as we go to step through the 1-D array. This approach has another variation, which is to calculate the source index explicitly rather than using an increment, for example:
for (int i = 0; i < M; ++ i) {
for (int j = 0; j < N; ++ j) {
int ctr = i * N + j;
a[i][j] = data[ctr];
}
}
The second approach is to instead iterate through the 1-D array, and calculate the destination position in the 2-D array. Modulo and integer division can help with that:
for (int ctr = 0; ctr < L; ++ ctr) {
int i = ctr / N;
int j = ctr % N;
a[i][j] = data[ctr];
}
All of these approaches work. Some may be more convenient than others depending on your situation. Note that the two explicitly calculated approaches can be more convenient if you have to do other transformations at the same time, e.g. the last approach above would make it very easy to, say, flip your 2-D matrix horizontally.
check this solution, it works for any length of data
public class ArrayTest
{
public static void main(String[] args)
{
int data[] = new int[] {10,20,30,40,50};
int length,limit1,limit2;
length=data.length;
if(length%2==0)
{
limit1=data.length/2;
limit2=2;
}
else
{
limit1=data.length/2+1;
limit2=2;
}
int data2[][] = new int[limit1][limit2];
int ctr=0;
//stores data in 2d array
for(int i=0;i<limit1;i++)
{
for(int j=0;j<limit2;j++)
{
if(ctr<length)
{
data2[i][j] = data[ctr];
ctr++;
}
else
{
break;
}
}
}
ctr=0;
//prints data from 2d array
for(int i=0;i<limit1;i++)
{
for(int j=0;j<limit2;j++)
{
if(ctr<length)
{
System.out.println(data2[i][j]);
ctr++;
}
else
{
break;
}
}
}
}
}