-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- A class called Function2D was created to allow several types of polynomial functions to be created and used in the aspherical surfaces. - The Poly1Drot was rewriten to use eigen instead of numpy ans some bugs were fixed, now is called PolyR - The aspheric_lesn.py (where the AsphericLens Component is defined), was fixed to use the new PolyR class - The import of several clases was fixed to follow the new structure Function2D->Poly2D - The Aperture class had a bug that created problems when used as a surface to define Components. This was fixed (the AsphericLensComponent may use Apertures in its definition) - The Cylinder class (used to define the AsphericLens) had a bug. It was corrected
- Loading branch information
1 parent
c7b1de1
commit b977f64
Showing
31 changed files
with
817 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from pyoptools.misc.cmisc.eigen cimport VectorXd, VectorXi, MatrixXd | ||
|
||
|
||
cdef class Function2D: | ||
""" | ||
Base class declaration for 2D functions defined in Cartesian coordinates. | ||
This file is for importing the `Function2D` API in other `.pyx` files. | ||
""" | ||
cdef double eval_cy(self, double x, double y) noexcept nogil | ||
cdef void eval2d(self, MatrixXd& x, MatrixXd& y, MatrixXd& result) noexcept nogil | ||
# def eval(self, double[:, ::1] x, double[:, ::1] y) | ||
cpdef tuple[Function2D, Function2D] dxdy(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
from pyoptools.misc.cmisc.eigen cimport MatrixXd | ||
from numpy import empty, float64 | ||
cimport cython | ||
|
||
cdef class Function2D: | ||
""" | ||
Abstract base class for 2D functions defined in Cartesian coordinates. | ||
This class provides a framework for representing and evaluating 2D mathematical | ||
functions, including their partial derivatives. All concrete implementations | ||
should inherit from this class and implement the required methods. | ||
Notes | ||
----- | ||
The class provides two main evaluation methods: | ||
- eval2d: A nogil Cython method using Eigen matrices | ||
- eval: A Python-accessible method using NumPy arrays | ||
Subclasses must implement: | ||
- eval_cy: Core evaluation function | ||
- dx: x-derivative | ||
- dy: y-derivative | ||
""" | ||
|
||
cdef void eval2d(self, MatrixXd& x, MatrixXd& y, MatrixXd& result) noexcept nogil: | ||
""" | ||
Evaluate the function for matrices of x and y coordinates using Eigen. | ||
Parameters | ||
---------- | ||
x : MatrixXd& | ||
Matrix of x coordinates | ||
y : MatrixXd& | ||
Matrix of y coordinates | ||
result : MatrixXd& | ||
Output matrix to store results | ||
Notes | ||
----- | ||
This is a nogil implementation using Eigen matrices for high performance. | ||
""" | ||
cdef Py_ssize_t i, j, n_cols, n_rows | ||
|
||
n_cols = x.cols() | ||
n_rows = x.rows() | ||
result.resize(n_rows, n_cols) | ||
|
||
for i in range(n_rows): | ||
for j in range(n_cols): | ||
(<double*>(&(result(i, j))))[0] = self.eval_cy(x(i, j), y(i, j)) | ||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def eval(self, double[:, ::1] x, double[:, ::1] y): | ||
""" | ||
Evaluate the function for arrays of x and y coordinates. | ||
Parameters | ||
---------- | ||
x : ndarray | ||
2D array of x coordinates | ||
y : ndarray | ||
2D array of y coordinates | ||
Returns | ||
------- | ||
ndarray | ||
2D array containing function values at each (x,y) point | ||
Notes | ||
----- | ||
This is the Python-accessible implementation using NumPy arrays. | ||
Decorated for maximum performance with bounds checking disabled. | ||
""" | ||
cdef int n_rows = x.shape[0] | ||
cdef int n_cols = x.shape[1] | ||
|
||
z_array = empty((n_rows, n_cols), dtype=float64) | ||
cdef double[:, ::1] z = z_array | ||
|
||
cdef int i, j | ||
for i in range(n_rows): | ||
for j in range(n_cols): | ||
z[i, j] = self.eval_cy(x[i, j], y[i, j]) | ||
|
||
return z_array | ||
|
||
cdef double eval_cy(self, double x, double y) noexcept nogil: | ||
""" | ||
Core evaluation method for a single point. | ||
Parameters | ||
---------- | ||
x : double | ||
x coordinate | ||
y : double | ||
y coordinate | ||
Returns | ||
------- | ||
double | ||
Function value at (x,y) | ||
Notes | ||
----- | ||
This method must be implemented by all subclasses. | ||
It is the fundamental evaluation method used by both eval() and eval2d(). | ||
""" | ||
with gil: | ||
raise NotImplementedError("Subclasses must implement eval_cy()") | ||
|
||
cpdef tuple[Function2D, Function2D] dxdy(self): | ||
""" | ||
Compute both partial derivatives simultaneously. | ||
Returns | ||
------- | ||
tuple[Function2D, Function2D] | ||
A tuple containing (∂f/∂x, ∂f/∂y) | ||
Notes | ||
----- | ||
This method must be implemented by all subclasses. | ||
""" | ||
raise NotImplementedError("Subclasses must implement dxdy()") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# from . import Poly2D, indices_to_powers, ord2i, pxpy2i | ||
|
||
# __all__ = ["Poly2D", "indices_to_powers", "ord2i", "pxpy2i"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from pyoptools.misc.cmisc.eigen cimport VectorXd, VectorXi, MatrixXd | ||
from ..function_2d cimport Function2D | ||
|
||
cdef class Poly2D(Function2D): | ||
""" | ||
Represents a 2D polynomial of the form: | ||
f(x, y) = Σ c[i] * x^(px[i]) * y^(py[i]) | ||
where px and py are the powers of x and y respectively for each term. | ||
""" | ||
|
||
# Polynomial coefficients and powers | ||
cdef VectorXi px # Power of x for each term | ||
cdef VectorXi py # Power of y for each term | ||
cdef VectorXd _coeff # Coefficients for each term | ||
cdef public int order # Order (degree) of the polynomial | ||
cdef int _num_coeff # Number of coefficients/terms in the polynomial | ||
|
||
# Cached derivatives | ||
cdef Poly2D dx # Cached derivative with respect to x (∂f/∂x) | ||
cdef Poly2D dy # Cached derivative with respect to y (∂f/∂y) | ||
|
||
# Evaluation methods | ||
cdef double eval_cy(self, double x, double y) noexcept nogil | ||
|
||
cpdef tuple[Function2D, Function2D] dxdy(self) | ||
|
||
# Utility functions | ||
cpdef int pxpy2i(int px, int py) | ||
cpdef int ord2i(int o) | ||
cpdef tuple[int, int] index_to_powers(int i) | ||
cpdef tuple indices_to_powers(int[:] indices) |
Oops, something went wrong.