jeudi 25 août 2022

How do I avoid duplicate Cartesian coordinates in my dynamically generated array? numpy.arange python 3.10

I am working to generate a list of Cartesian coordinates in python3.10. The problem is, that of the 16 individual coordinates generated, the second 8 coordinate sets are identical to the first 8 coordinate sets. How do I ensure that all 16 coordinate sets are unique? My solution needs to scale up to 1,024 coordinate sets. I coupled numpy.arange() with a random number generator to get the desired array size. The duplication only seems to occur after I perform matrix multiplication to reduce the matrix from 4 dimensions to 3. Here is the code I currently have, and it's outputs:

import numpy as np

dims = 4         # User can enter a value between 4 and 10. 
magnitude = 1    # User can enter any number here.

verticesMatrix = magnitude * (2*((np.arange(2**dimensions)[:,None] & (1 << np.arange(dimensions))) > 0) - 1)

rows = int(0)
cols = int(0)

projectionMatrix = np.ndarray(shape=(dimensions-1, dimensions), dtype=np.int32)
    
for rows in range(dimensions-1):
    for cols in range(dimensions):
        if cols == rows:
            projectionMatrix[rows, cols] = 1
        else:
            projectionMatrix[rows, cols] = 0

myProjection = matrices.projectionMatrix(dims)
    myProjectionRows, myProjectionCols = np.shape(myProjection)
    myVerticesRows, myVerticesAxes = originalVertices[0].shape
    
    if myProjectionCols != myVerticesAxes:
        logging.error("# of Columns in Projection Matrix must be the same as the # of axes in 
                       each coordinate set from the Vertices Matrix.")
    else:
        myFlattenedHypercube = np.empty((myVerticesRows, myProjectionRows))
        row = 0
        while row <= vertices - 1:
            myFlattenedHypercube[row] = np.matmul(myProjection, originalVertices[0][row])
            row += 1

The output that I get in my log is as follows:

('Orginal VerticesMatrix Generated:', array(
  [[-1, -1, -1, -1],
   [ 1, -1, -1, -1],
   [-1,  1, -1, -1],
   [ 1,  1, -1, -1],
   [-1, -1,  1, -1],
   [ 1, -1,  1, -1],
   [-1,  1,  1, -1],
   [ 1,  1,  1, -1],
   [-1, -1, -1,  1],
   [ 1, -1, -1,  1],
   [-1,  1, -1,  1],
   [ 1,  1, -1,  1],
   [-1, -1,  1,  1],
   [ 1, -1,  1,  1],
   [-1,  1,  1,  1],
   [ 1,  1,  1,  1]]))

('Projection Matrix:', array(
  [[1, 0, 0, 0],
   [0, 1, 0, 0],
   [0, 0, 1, 0]]))

("Returned flattened vertices:", array(
  [[-1., -1., -1.],
   [ 1., -1., -1.],
   [-1.,  1., -1.],
   [ 1.,  1., -1.],
   [-1., -1.,  1.],
   [ 1., -1.,  1.],
   [-1.,  1.,  1.],
   [ 1.,  1.,  1.],     # This is the 8th coordinate set
   [-1., -1., -1.],
   [ 1., -1., -1.],
   [-1.,  1., -1.],
   [ 1.,  1., -1.],
   [-1., -1.,  1.],
   [ 1., -1.,  1.],
   [-1.,  1.,  1.],
   [ 1.,  1.,  1.]]))   # This is the 16th coordinate set, and is identical to the 8th.

I am just a poor fool subject to the laws of matrix multiplication yielding such an unfortunate result?




Aucun commentaire:

Enregistrer un commentaire