java代碼
package top.gunplan.services;
import top.gunplan.RPC.APIS.test.LinearAlgebra;
/**
* LinearAlgebra function lib
*/
public class LinearAlgebraImpl implements LinearAlgebra {
{
System.loadLibrary("LinearAlgebraLib");
}
public native int calDet();
/**
* o(n^3)
* client c
*
* @param a det
* @param s det's size
* @return calu resu
*/
@Override
public CalRes calDet(int[][] a, int s) {
if (c(a, s)) {
float[][] d = fp(a,s);
for (int i = 0; i < s; i++) {
for (int j = i + 1; j < s; j++) {
ro(d, i, j, s);
}
}
return new CalRes(true, ot(d));
} else {
return new CalRes(false, -2011230);
}
}
private boolean c(int[][] a, int size) {
if (a.length != size) {
return false;
}
for (int[] l : a) {
if (l.length != size) {
return false;
}
}
return true;
}
private float[][] fp(int[][] a, int l) {
float[][] fp = new float[l][l];
for (int i = 0; i < l; i++) {
for (int j = 0; j < l; j++) {
fp[i][j] = a[i][j];
}
}
return fp;
}
private void ro(float[][] a, int i, int j, int p) {
float ptr = a[i][i];
float c = a[j][i] / ptr;
for (int k = 0; k < p; k++) {
a[j][k] = (a[j][k] - a[i][k] * c);
}
}
private int ot(float[][] a) {
float val = 1;
for (int i = 0; i < a.length; i++) {
val *= a[i][i];
}
return (int) val;
}
}
服務接口
package top.gunplan.RPC.APIS.test;
import top.gunplan.RPC.APIS.test.anno.GunUseImpl;
import java.io.Serializable;
/**
* @author dosdrtt
* @date 1557231535
*/
@GunUseImpl(impl = "top.gunplan.services.LinearAlgebraImpl")
public interface LinearAlgebra {
/**
* calculator the det value
* @param a det
* @param size det's size
* @return calres
*/
CalRes calDet(int[][] a, int size);
class CalRes implements Serializable {
private static final long serialVersionUID = -8759739552340900011L;
boolean isTrueCal;
int calResult;
public CalRes(boolean isTrueCal, int calResult) {
this.isTrueCal = isTrueCal;
this.calResult = calResult;
}
public boolean isTrueCal() {
return isTrueCal;
}
public void setTrueCal(boolean trueCal) {
isTrueCal = trueCal;
}
public int getCalResult() {
return calResult;
}
public void setCalResult(int calResult) {
this.calResult = calResult;
}
}
}
閱讀更多 青峰科技 的文章
關鍵字: Java