2

Suppose you have a set of numbers in a given domain, for example: [-4,4]

Also suppose that this set of numbers is in an array, and in numerical order, like so:

[-4, -3 -2, -1, 0, 1, 2, 3, 4]

Now suppose I would like to create a new zero-point for this set of numbers, like so: (I select -2 to be my new axis, and all elements are shifted accordingly)

Original: [-4, -3 -2, -1, 0, 1, 2, 3, 4]

Zeroed: [-2, -1 0, 1, 2, 3, 4, -4, -3]

With the new zeroed array, lets say I have a function called:

"int getElementRelativeToZeroPosition(int zeroPos, int valueFromOriginalArray, int startDomain, int endDomain) {...}"

with example usage:

I am given 3 of the original array, and would like to see where it mapped to on the zeroed array, with the zero on -2.

getElementRelativeToZeroPosition(-2, 3, -4, 4) = -4

Without having to create any arrays and move elements around for this mapping, how would I mathematically produce the desired result of the function above?

0

3 Answers 3

2

I would proceed this way:

  1. Get index of original zero position
  2. Get index of new zero position (ie. index of -2 in you example)
  3. Get index of searched position (index of 3)
  4. Compute move vector between new and original zero position
  5. Apply move vector to searched position modulo the array size to perform the rotation

Provided your array is zero-based:

index(0) => 4
index(-2) => 2
index(3) => 7
array_size => 9

move_vector => index(0) - index(-2)
            => 4 - 2 => +2

new_pos(3) => (index(3) + move_vector) modulo array_size
           => (7 + 2) mod 9 => 0

value_at(0) => -4

That's it

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

Comments

1

Mathematically speaking, if you have an implicit set of integers given by an inclusive range [start, stop], the choice of choosing a new zero point is really a choosing of an index to start at. After you compute this index, you can compute the index of your query point (in the original domain), and find the difference between them to get the offset:

For example:

  • Given: range [-4, 4], assume zero-indexed array (0,...,8) corresponding to values in the range
    • length(range) = 4 - (-4) + 1= 9
  • Choose new 'zero point' of -2.
    • Index of -2 is -2 - (-4) = -2 + 4 = 2
  • Query for position of 3:
    • Index in original range: 3 - (-4) = 3 + 4 = 7
  • Find offset of 3 in zeroed array:
    • This is the difference between the indices in the original array
    • 7 - 2 = 5, so the element 3 is five hops away from element -2. Equivalently, it's 5-len(range) = 5 - 9 = -4 hops away. You can take the min(abs(5), abs(-4)) to see which one you'd prefer to take.

Comments

0

you can write a doubled linked list, with a head-node which points to the beginning

struct nodeItem
{
    nodeItem* pev = nullptr;
    nodeItem* next = nullptr;
    int value = 0;
}

class Node
{
private:
    nodeItem* head;

public:
    void SetHeadToValue(int value);
    ...
}

The last value should point with next to the first one, so you have a circular list.

To figur out, if you are at the end of the list, you have to check if the item is equal to the head node

3 Comments

I wouldn't say that this is necessarily a mathematical solution. I'm looking for efficiency and would prefer to perform simple mathematical operations than to eat-up resources. Nonetheless I will take this into consideration as an option.
the question is: how are you using it? so for what are you using it?
if it is only for drawing from different startposition, then this kind of calculation is fast, if you want to add new items into, it is also very fast, if you are jumping on random indicies it is realy slow...

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.