add pinv function

This commit is contained in:
wyq 2023-03-07 16:13:20 +08:00
parent 87a10cb9f2
commit 376df7f35d
10 changed files with 141 additions and 27 deletions

View File

@ -340,6 +340,8 @@
<option name="ignoredErrors">
<list>
<option value="N802" />
<option value="N803" />
<option value="N806" />
</list>
</option>
</inspection_tool>

View File

@ -1,32 +1,36 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\io\micaps">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\stats"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\interpolate"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\test"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\others"/>
<Path OpenPath="D:\Working\MIScript\Jython\mis\common_math\linalg">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\scatter"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\data_process"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\new"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\dataframe"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\micaps"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\model"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\linalg"/>
</Path>
<File>
<OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\model\obj_Lion_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\hist\hist_h_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\micaps\micaps_4_9.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\model\obj_car_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\model\obj_airplane_line.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\linalg\inv_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\linalg\pinv_1.py"/>
</OpenedFiles>
<RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\model\obj_Lion_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\hist\hist_h_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\io\micaps\micaps_4_9.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\model\obj_car_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\model\obj_airplane_line.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\linalg\inv_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\linalg\pinv_1.py"/>
</RecentFiles>
</File>
<Font>
@ -34,5 +38,5 @@
</Font>
<LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/>
<Figure DoubleBuffering="true"/>
<Startup MainFormLocation="-7,0" MainFormSize="1348,763"/>
<Startup MainFormLocation="-7,0" MainFormSize="1376,806"/>
</MeteoInfo>

View File

@ -12,10 +12,8 @@ from org.meteoinfo.math.stats import StatsUtil
from .. import core as np
__all__ = [
'solve', 'cholesky', 'det', 'lu', 'qr', 'svd', 'eig', 'inv', 'lstsq', 'slogdet', 'solve_triangular',
'norm'
]
__all__ = ['solve', 'cholesky', 'det', 'lu', 'qr', 'svd', 'eig', 'inv', 'lstsq', 'slogdet',
'solve_triangular', 'norm', 'pinv']
class LinAlgError(Exception):
@ -268,6 +266,19 @@ def inv(a):
return np.NDArray(r)
def pinv(a):
"""
Compute the pseudo inverse of a matrix.
:param a: (*array_like*) Input array.
:returns: Pseudo inverse matrix.
"""
a = np.asarray(a)
r = LinalgUtil.pinv(a.asarray())
return np.NDArray(r)
def lstsq(a, b):
"""
Compute least-squares solution to equation Ax = b.

View File

@ -1355,6 +1355,46 @@ class Axes3DGL(Axes3D):
return graphics
def model(self, T, x, y, z, normal=None, **kwargs):
"""
Model plot.
:param T: (*array*) Triangle connectivity, specified as a 3-column matrix where each
row contains the point vertices defining a triangle face.
:param x: (*array*) X coordinates array.
:param y: (*array*) Y coordinates array.
:param z: (*array*) Z coordinates array.
:param normal: (*array*) Normal array. Default is `None`.
:return: Triangle mesh graphic.
"""
facecolor = kwargs.pop('facecolor', 'c')
facecolor = plotutil.getcolor(facecolor)
ls = LegendManage.createSingleSymbolLegendScheme(ShapeTypes.POLYGON, facecolor, 1)
if 'edgecolor' not in kwargs.keys():
kwargs['edgecolor'] = None
plotutil.setlegendscheme(ls, **kwargs)
if normal is None:
graphics = GraphicFactory.model(T._array, x._array, y._array, z._array, ls)
else:
graphics = GraphicFactory.model(T._array, x._array, y._array, z._array,
normal._array, ls)
angle = kwargs.pop('angle', None)
if angle is not None:
graphics.setAngle(angle)
scale = kwargs.pop('scale', None)
if scale is not None:
graphics.setScale(scale)
visible = kwargs.pop('visible', True)
if visible:
self.add_graphic(graphics)
return graphics
def fimplicit3(self, f, interval=[-5., 5.], mesh_density=35, *args, **kwargs):
"""
Plot the 3-D implicit function defined by f(x,y,z) = 0 over the default interval [-5, 5] for x, y, and z.

View File

@ -48,7 +48,7 @@ __all__ = [
'webmap', 'gca', 'gcf', 'gc_collect', 'geoshow', 'get_figure', 'gifaddframe', 'gifanimation', 'giffinish',
'grid', 'gridshow', 'gridshowm', 'hist', 'imshow', 'imshowm', 'isosurface', 'legend', 'left_title', 'lighting',
'loglog', 'makecolors',
'makelegend', 'makesymbolspec', 'masklayer', 'material', 'mesh', 'particles', 'pcolor', 'pcolorm', 'pie', 'plot',
'makelegend', 'makesymbolspec', 'masklayer', 'material', 'mesh', 'model', 'particles', 'pcolor', 'pcolorm', 'pie', 'plot',
'plot3', 'plotm', 'quiver', 'quiver3',
'quiverkey', 'quiverm', 'readlegend', 'right_title', 'refresh', 'savefig', 'savefig_jpeg', 'scatter', 'scatter3',
'scatterm',
@ -2013,6 +2013,20 @@ def trisurf(T, x, y, z, normal=None, **kwargs):
return r
@_copy_docstring_and_deprecators(Axes3DGL.model)
def model(T, x, y, z, normal=None, **kwargs):
global g_axes
if g_axes is None:
g_axes = axes3d()
else:
if not isinstance(g_axes, Axes3DGL):
g_axes = axes3dgl()
r = g_axes.model(T, x, y, z, normal, **kwargs)
draw_if_interactive()
return r
@_copy_docstring_and_deprecators(Axes3DGL.slice)
def slice3(*args, **kwargs):
global g_axes

View File

@ -214,6 +214,44 @@ public class LinalgUtil {
return r == null ? null : MatrixUtil.matrixToArray(r);
}
/**
* Calculate pseudo inverse matrix
*
* @param a The matrix
* @return Pseudo inverse matrix array
*/
public static Array pinv(Array a) {
double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray_Double(a);
RealMatrix matrix = new Array2DRowRealMatrix(aa, false);
SingularValueDecomposition svd = new SingularValueDecomposition(matrix);
DecompositionSolver solver = svd.getSolver();
RealMatrix pinv = solver.getInverse();
int n = pinv.getColumnDimension();
int m = pinv.getRowDimension();
Array r = Array.factory(DataType.DOUBLE, new int[]{m, n});
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
r.setDouble(i * n + j, pinv.getEntry(i, j));
}
}
return r;
}
/**
* Calculate pseudo inverse matrix
*
* @param a The matrix
* @return Pseudo inverse matrix array
*//*
public static Array pinv(Array a) {
Matrix ma = MatrixUtil.arrayToMatrix(a);
Matrix.SVD svd = ma.svd();
Matrix r = svd.pinv();
return r == null ? null : MatrixUtil.matrixToArray(r);
}*/
/**
* Compute the determinant of an array
* @param a The matrix array

View File

@ -1645,31 +1645,35 @@ public class Matrix extends DMatrix {
* Returns {@code A * D * B}, where D is a diagonal matrix.
* @param transA normal, transpose, or conjugate transpose
* operation on the matrix A.
* @param A the operand.
* @param D the diagonal matrix.
* @param transB normal, transpose, or conjugate transpose
* operation on the matrix B.
* @param B the operand.
* @param diag the diagonal matrix.
* @return the multiplication.
*/
public Matrix adb(Transpose transA, Transpose transB, Matrix B, double[] diag) {
Matrix C;
public static Matrix adb(Transpose transA, Matrix A, double[] D, Transpose transB, Matrix B) {
Matrix AD;
int m = A.m, n = A.n;
if (transA == NO_TRANSPOSE) {
C = new Matrix(m, n);
AD = new Matrix(m, n);
for (int j = 0; j < n; j++) {
double dj = D[j];
for (int i = 0; i < m; i++) {
C.set(i, j, diag[j] * get(i, j));
AD.set(i, j, dj * A.get(i, j));
}
}
} else {
C = new Matrix(n, m);
AD = new Matrix(n, m);
for (int j = 0; j < m; j++) {
double dj = D[j];
for (int i = 0; i < n; i++) {
C.set(i, j, diag[j] * get(j, i));
AD.set(i, j, dj * A.get(j, i));
}
}
}
return transB == NO_TRANSPOSE ? C.mm(B) : C.mt(B);
return transB == NO_TRANSPOSE ? AD.mm(B) : AD.mt(B);
}
/**
@ -2179,7 +2183,8 @@ public class Matrix extends DMatrix {
sigma[i] = 1.0f / s[i];
}
return V.adb(NO_TRANSPOSE, TRANSPOSE, U, sigma);
//return V.adb(NO_TRANSPOSE, TRANSPOSE, U, sigma);
return adb(NO_TRANSPOSE, V, sigma, TRANSPOSE, U);
}
/**