0

I have this piece of code, within my Main method:

for(int i = 0; i < 2; i++){
            double psd = JMath.sqrt((((4*cc)/(JMath.pow((1 + 6*frequency[i]*cc), (double) 5/3)))*df));              
            double cohC = H*JMath.sqrt((frequency[i]/A.averageHubWindSpeed)*(frequency[i]/A.averageHubWindSpeed) + (.12/Lc)*(.12/Lc));          
            BLAS.getInstance().sscal(packDistance.length, (float) cohC,packDistance, 1);    
}

This way packDistance is overwritten, so for i == 1, sscal will multiply cohC with the packDistance stemming from the sscal at i == 0. Instead, I want packDistance to hold the same original values, which are assigned outside the loop.

How could I overtake this issue?

4
  • 2
    Use a copy of the array made before the loop. Commented Jun 28, 2013 at 10:06
  • Well, copy the array to another before entering the for loop and use that copy. Look at Arrays.copyOf(). Commented Jun 28, 2013 at 10:06
  • Put that array assignment statement inside the loop. Commented Jun 28, 2013 at 10:06
  • Maybe it's just me, but I am unable to understand what you're trying to do. A more complete (self-contained) code sample and explanation with examples of desired value and actual value always helps. And your code seems unnecessarily complicated to reproduce the problem - you surely could've replaced the two equations with something a lot simpler. Commented Jun 28, 2013 at 10:12

1 Answer 1

2

You can use java's Arrays.copyOf() method as follows :

T[] arr2 = Arrays.copyOf(packDistance,packDistance.length);
for(int i = 0; i < 2; i++){
            double psd = JMath.sqrt((((4*cc)/(JMath.pow((1 + 6*frequency[i]*cc), (double) 5/3)))*df));              
            double cohC = H*JMath.sqrt((frequency[i]/A.averageHubWindSpeed)*(frequency[i]/A.averageHubWindSpeed) + (.12/Lc)*(.12/Lc));          
            BLAS.getInstance().sscal(packDistance.length, (float) cohC,packDistance, 1);    
}

where T is the data type of the array you are trying to copy. Here is an example of usage of Arrays.copyOf().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.