mercredi 10 juillet 2019

Avoiding nested if statements with numpy array

I'm attempting to convert coordinate systems and then evaluate which coordinates are defined in a GUI. This function will later be built out for that. Currently, I'm attempting to avoid nested if statements.

Essentially, would like to evaluate if the np.array is empty or not. Then evaluate the variable, with those points, to check if they are empty. If they are empty, then evaluate the statements within. I'm trying to avoid more nested if statements, as well as an unnecessary amount of shared data between variables, if possible. Any suggestions?

I've tried building multiple variables and then comparing those. Although this isn't necessarily what I would like either.

#!/bin/python3
import numpy as np
import math as m
from ai import cs

# Functions
def cart_2_cyl(pts):
    #Conversion - Start Cartesian (Cartesian to Cylindrical)
    pts[1,0], pts[1,1], pts[1,2] = cs.cart2cyl(pts[0,0],pts[0,1],pts[0,2])
    #Read
    pos_cartcyl_cart = pts[1,0], pts[1,1], pts[1,2]
    return(pos_cartcyl_cart)

def cart_2_sph(pts):

    #Conversion - Start Spherical(Cartesian to Spherical)
    pts[2,0], pts[2,1] , pts[2,2] = cs.cart2sp(pts[0,0], pts[0,1], pts[0,2])
    #Read
    pos_cartsph_cart = pts[2,0], pts[2,1], pts[2,2]   
    return(pos_cartsph_cart)


def sph_2_cart(pts):
    #Conversion - Start Spherical(Spherical to Cartesian)
    pts[0,0], pts[0,1], pts[0,2] = cs.sp2cart(pts[2,0], pts[2,1], pts[2,2])
    #Read
    pos_sp2cart_sph = pts[0,0], pts[0,1], pts[0,2]
    return(pos_sp2cart_sph)

def cyl_2_cart(pts):
    #Conversion - Start Cylindrical (Cylindrical to Cartesian)
    pts[0,0], pts[0,1], pts[0,2] = cs.cyl2cart(pts[1,0], pts[1,1], pts[1,2])
    #Read
    pos_cylcart_cyl = pts[0,0], pts[0,1], pts[0,2]
    return(pos_cylcart_cyl)

ThreeThree = (3,3)

pts = np.empty(ThreeThree)
pts = np.empty(ThreeThree)
pts = np.empty(ThreeThree)

cart = np.empty((1,3))
cyl = np.empty((1,3))
sph = np.empty((1,3))

# pts[2,0], pts[2,1], pts[2,2] = 1, 1, 1
# cart =  np.array((pts[2,0], pts[2,1], pts[2,2]))

pts[1,0], pts[1,1], pts[1,2] = 1, 1, 1
cyl =  np.array((pts[1,0], pts[1,1], pts[1,2]))

# pts[2,0], pts[2,1], pts[2,2] = 1, 1, 1
# sph =  np.array((pts[2,0], pts[2,1], pts[2,2]))

if not cart.size == 0:
    # Check Cartesian
    if cart.size == 0:  #If cart array is empty -> Return True
        print("\nCartesian:", cart)
        if not cyl.any():
            print("Cylindrical Coordinates are Empty")
        else:

            print("Cylindrical:", cart_2_cyl(pts))

        if not sph.any():
            print("Spherical Coordinates are Empty")
        else:
            print("Spherical:", cart_2_sph(pts),'\n') 

    if not cyl.size == 0:
        print("\nCylindrical:", cyl)
        # Check Spherical
        if not sph.any():  #If sph array is empty -> Return True
            print("Spherical Coordinates are Empty")
        else:   
            cyl_2_cart(pts)
            print("Spherical:", cart_2_sph(pts))
        if not cart.any():
            print("\nCartesian Coordinates are Empty")
        else:
            print("Cartesian:", cyl_2_cart(pts),'\n')

    if not sph.size == 0:
        print("\n Spherical:", sph)
        # Check Spherical
        if not cyl.any():  #If sph array is empty -> Return True
            print("Cylindrical Coordinates are Empty")
        else:   
            sph_2_cart(pts)
            print("Cylindrical:", cart_2_sph(pts))
        if not cart.any():
            print("\nCartesian Coordinates are Empty")
        else:
            print("Cartesian:", cyl_2_cart(pts),'\n')

else:   
    print('Unsuccessful')

I'm expecting only one if (cart, cyl, sph).size == 0 to be evaluated, given the variable being set.

Any suggestions would be appreciated, thanks.

Aucun commentaire:

Enregistrer un commentaire