Mathematica Circular Shift of Array Elements

Click For Summary

Discussion Overview

The discussion revolves around finding an efficient method to circularly shift elements in an array using Mathematica, particularly for large datasets. Participants explore functions available in Mathematica, troubleshoot issues related to matrix operations, and seek solutions for data manipulation and plotting.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about a Mathematica function for circularly shifting array elements without iteration, citing performance concerns with large datasets.
  • Another participant suggests using the functions RotateRight and RotateLeft for the circular shift operation.
  • The original poster confirms that the suggested functions work for individual matrix elements but encounters recursion limit errors when simplifying their loop.
  • It is noted that the size of the vectors (35,000 elements) may contribute to performance issues.
  • A participant identifies that the problem may stem from matrix multiplication errors due to incompatible shapes of the arrays involved.
  • There is a realization that Mathematica does not automatically transpose for dot products, leading to a need for manual transposition of column vectors.
  • The original poster expresses a need to convert a 2D array with one column into a vector to resolve dimensionality issues for plotting.
  • Another participant suggests using the Flatten function to convert the array into a vector.
  • The original poster later discusses difficulties in constructing a 2D array for plotting, indicating issues with dimensions when combining time and correlationFunction data.
  • Finally, the original poster resolves a problem related to the length of the time vector affecting data structure.

Areas of Agreement / Disagreement

Participants generally agree on the utility of the RotateLeft and RotateRight functions for circular shifts. However, there are unresolved issues regarding matrix operations, dimensionality, and data structure manipulation, indicating multiple competing views and approaches.

Contextual Notes

Participants mention limitations related to recursion depth and the handling of large datasets, as well as unresolved questions about the behavior of Mathematica with 2D arrays and vector operations.

rynlee
Messages
44
Reaction score
0
Hi All,

does anyone know of a mathematica function that efficiently circularly shifts all array elements?

e.g.
{1,2,3,4,5} -> {4,5,1,2,3}

without iterating? The reason I ask is because I have a huge data set I'm working with and right now iterating through is very processing intensive and is taking forever. I figured out a way to do what I need to do however via vector operations without the need to iterate, which I suspect may be a more efficient process in the mathematica back end, but to do so I need to perform this circular transformation first, and iterating through each element would defeat the purpose.

Thanks,
ryn
 
Physics news on Phys.org
There are two functions RotateRight and RotateLeft
 
that should work perfectly, thanks
 
So, that appears to be working (when I test individual matrix elements, the functions RotateLeft and RotateRight work perfectly).

Unfortunately, when I try to simplify my loop, I get thrown tons of errors like these:
$RecursionLimit::reclim:Recursion Depth of 256 exceeded

which I didn't get before. My loop is now this:

Code:
Do[
 delayDeltaOmega = RotateLeft[deltaOmega, a - 1]
    correlationFunction[[a]] = delayDeltaOmega.deltaOmega
 , {a, 1, length/5, 1}]

the actual variable length/5 doesn't matter (it's an integer, and I checked it works earlier). Is something wrong elsewhere?

Thanks for taking a look,

Ryn
 
I should say that the issue may be that each vector has 35000 elements. That's certainly why it was taking so long before. The 'length' by the way is 35000 and deltaOmega is just a 35000x1 array.

Thanks,
Ryn
 
OK, I tried it without the line:

correlationFunction[[a]] = delayDeltaOmega.deltaOmega;

and it worked fine, so my problem must be with the matrix multiplication
 
delayDeltaOmega.deltaOmega gives this error:

Dot::dotsh: Tensors {{-1.37},{-2.37},{-2.37},{-2.37},{-3.37},{-1.37},<<39>>,{-8.37},{-5.37},{-7.37},{-6.37},{-6.37},<<65486>>} and {{10.6},{9.63},{8.63},{6.63},{3.63},{3.63},<<39>>,{5.63},{6.63},{6.63},{6.63},{6.63},<<65486>>} have incompatible shapes. >>
 
AHA!

I thought mathematica automatically transposed for dot products, it does not, I needed to transpose my column vector. problem solved for now
 
Mathematica does not automatically transpose if you make your array a 2D array, but if you simply use 1D vectors then you don't have to transpose.
 
  • #10
Hm, it seems then that I'm dealing with a 2D array with only one column. When I call

Dimensions[deltaOmega] prior to the loop, I get

{big number, 1}

This seems to then be causing me trouble later, because after I do the multiplication (it is much faster now btw), I get dimensions of the resulting correlationFunction

Dimensions[correlationFunction]
{big number, 1, 1}

This is problematic, as it makes it hard for me to plot it.

Is there a way I can turn my array (which came from the data I imported for some reason it was in that form) into a vector? Would that solve my problems?

Vectors aside, multiplying a dimensions {x,1} array by a dimensions Transpose[{x,1}] array should give me just a scalar shouldn't it? Is something odd happening with my dimensions in

Code:
Do[
 delayDeltaOmega = RotateLeft[deltaOmega, a - 1]
    correlationFunction[[a]] = delayDeltaOmega.deltaOmega
 , {a, 1, length/5, 1}]

?

When I define correlationFunction I give it as:

Code:
correlationFunction = Array[0 &, {length/5, 1}];
 
  • #11
rynlee said:
Is there a way I can turn my array (which came from the data I imported for some reason it was in that form) into a vector?
Just use Flatten
 
Last edited:
  • #12
Thanks, that works nicely.

Now I'm running into a different issue, for ListPlot I'm trying construct a dimension {big number,2} array to give to ListPlot with one column the x-axis numbers and the other column the y-axis numbers, but I'm having trouble doing that with things being vectors. I've tried:

{time, correlationFunction}, but for some reason that gives dimensions {2}
 
  • #13
I tried a number of things, even something like this:

Code:
dataPlot = Array[0 &, {length/5, 2}];
dataPlot[[All, 1]] = time;
dataPlot[[All, 2]] = correlationFunction;

which gives me dimensions {big number, 2}, but when I try to look at values, for example row 20,

Code:
dataPlot[[20]]

I don't get a pair of numbers, I get all the numbers somehow.

Also

Code:
Transpose[{time, correlationFunction}]

also does not work, it gives me dimensions {1}
 
Last edited:
  • #14
Nevermind, the length of the time vector was too long, I fixed it and things seem to work now.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
7K
Replies
43
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 30 ·
2
Replies
30
Views
7K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K