Search code examples
pythonarraystransformdimensions

Mapping array into higher-dimension - Python


I've to map R^2 array X=[x1, x2] into R^3 array X'=[x1, x2, x1^2].

Is there any compact solution?

ADDED: My starting array is a numpy array e.g. x = numpy.array([2, 3]). I want to end up with another numpy array e.g. x' = numpy.array([2, 3, 4]).


Solution

  • For the case where your array is a numpy array with shape (2,), one way to add the additional member is

    xx = numpy.append(x, x[0]**2)
    

    Unfortunately, if you try something like x + [x[0]**2] numpy will add the elements together rather than append the new element. If you try something like [x[0], x[1], x[0]**2] you end up with a list rather than a numpy array.