복붙노트

[PYTHON] 좌표 사이의 파이썬 변환

PYTHON

좌표 사이의 파이썬 변환

서로 다른 좌표계간에 변환 기능이 있습니까?

예를 들어, Matlab은 직교 좌표로부터 극좌표 좌표로의 변환을 위해 ρφ = cart2pol (x, y)를 갖는다. 그것이 numpy 또는 scipy에 있어야하는 것처럼 보입니다.

해결법

  1. ==============================

    1.numpy를 사용하면 다음을 정의 할 수 있습니다.

    numpy를 사용하면 다음을 정의 할 수 있습니다.

    import numpy as np
    
    def cart2pol(x, y):
        rho = np.sqrt(x**2 + y**2)
        phi = np.arctan2(y, x)
        return(rho, phi)
    
    def pol2cart(rho, phi):
        x = rho * np.cos(phi)
        y = rho * np.sin(phi)
        return(x, y)
    
  2. ==============================

    2.기존 답변을 단순화 할 수 있습니다.

    기존 답변을 단순화 할 수 있습니다.

    from numpy import exp, abs, angle
    
    def polar2z(r,theta):
        return r * exp( 1j * theta )
    
    def z2polar(z):
        return ( abs(z), angle(z) )
    

    또는:

    polar2z = lambda r,θ: r * exp( 1j * θ )
    z2polar = lambda z: ( abs(z), angle(z) )
    

    이것들은 배열에서도 작동합니다!

    rS, thetaS = z2polar( [z1,z2,z3] )
    zS = polar2z( rS, thetaS )
    
  3. ==============================

    3.당신이 numpy 또는 scipy로 그것을 찾을 수 없다면, 여기에 몇 가지 빠른 기능과 포인트 클래스가 있습니다 :

    당신이 numpy 또는 scipy로 그것을 찾을 수 없다면, 여기에 몇 가지 빠른 기능과 포인트 클래스가 있습니다 :

    import math
    
    def rect(r, theta):
        """theta in degrees
    
        returns tuple; (float, float); (x,y)
        """
        x = r * math.cos(math.radians(theta))
        y = r * math.sin(math.radians(theta))
        return x,y
    
    def polar(x, y):
        """returns r, theta(degrees)
        """
        r = (x ** 2 + y ** 2) ** .5
        if y == 0:
            theta = 180 if x < 0 else 0
        elif x == 0:
            theta = 90 if y > 0 else 270
        else:
            theta = math.degrees(math.atan(float(y) / x))
        return r, theta
    
    class Point(object):
        def __init__(self, x=None, y=None, r=None, theta=None):
            """x and y or r and theta(degrees)
            """
            if x and y:
                self.c_polar(x, y)
            elif r and theta:
                self.c_rect(r, theta)
            else:
                raise ValueError('Must specify x and y or r and theta')
        def c_polar(self, x, y, f = polar):
            self._x = x
            self._y = y
            self._r, self._theta = f(self._x, self._y)
            self._theta_radians = math.radians(self._theta)
        def c_rect(self, r, theta, f = rect):
            """theta in degrees
            """
            self._r = r
            self._theta = theta
            self._theta_radians = math.radians(theta)
            self._x, self._y = f(self._r, self._theta)
        def setx(self, x):
            self.c_polar(x, self._y)
        def getx(self):
            return self._x
        x = property(fget = getx, fset = setx)
        def sety(self, y):
            self.c_polar(self._x, y)
        def gety(self):
            return self._y
        y = property(fget = gety, fset = sety)
        def setxy(self, x, y):
            self.c_polar(x, y)
        def getxy(self):
            return self._x, self._y
        xy = property(fget = getxy, fset = setxy)
        def setr(self, r):
            self.c_rect(r, self._theta)
        def getr(self):
            return self._r
        r = property(fget = getr, fset = setr)
        def settheta(self, theta):
            """theta in degrees
            """
            self.c_rect(self._r, theta)
        def gettheta(self):
            return self._theta
        theta = property(fget = gettheta, fset = settheta)
        def set_r_theta(self, r, theta):
            """theta in degrees
            """
            self.c_rect(r, theta)
        def get_r_theta(self):
            return self._r, self._theta
        r_theta = property(fget = get_r_theta, fset = set_r_theta)
        def __str__(self):
            return '({},{})'.format(self._x, self._y)
    
  4. ==============================

    4.좌표가 복소수로 저장되어 있으면 cmath를 사용할 수 있습니다.

    좌표가 복소수로 저장되어 있으면 cmath를 사용할 수 있습니다.

  5. ==============================

    5.cmath 모듈을 사용할 수 있습니다.

    cmath 모듈을 사용할 수 있습니다.

    숫자가 복잡한 형식으로 변환되면 숫자에 극좌표 메서드를 호출하는 것이 더 쉬워집니다.

    import cmath
    input_num = complex(1, 2) # stored as 1+2j
    r, phi = cmath.polar(input_num)
    
  6. ==============================

    6.polar ()을 작성하는 더 좋은 방법이 있습니다. 여기 있습니다 :

    polar ()을 작성하는 더 좋은 방법이 있습니다. 여기 있습니다 :

    def polar(x,y):
      `returns r, theta(degrees)`
      return math.hypot(x,y),math.degrees(math.atan2(y,x))
    
  7. ==============================

    7.일반적으로 생각해 보면 잘 디자인 된 추상화 뒤에 좌표 시스템을 숨기는 것이 좋습니다. 삼촌 밥과 그의 책을 인용 :

    일반적으로 생각해 보면 잘 디자인 된 추상화 뒤에 좌표 시스템을 숨기는 것이 좋습니다. 삼촌 밥과 그의 책을 인용 :

    class Point(object)
        def setCartesian(self, x, y)
        def setPolar(self, rho, theta)
        def getX(self)
        def getY(self)
        def getRho(self)
        def setTheta(self)
    

    Point 클래스의 모든 사용자가 편리한 표현을 선택할 수있는 인터페이스를 사용하면 명시 적 변환이 수행되지 않습니다. 이 모든 추악한 사인, 코사인 등은 한 곳에 숨겨져 있습니다. Point 클래스. 컴퓨터 메모리에서 어떤 표현이 사용되는지주의해야하는 곳에 만 두십시오.

  8. from https://stackoverflow.com/questions/20924085/python-conversion-between-coordinates by cc-by-sa and MIT license