package excel;
import java.util.*;
/**
* <p>基本函数集</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2010</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class MathFun {
public static double insert2(double x0, double x[], double y[], int n) { //两点插值(double)
if (n <= 1) {
return 0;
}
int i;
double y0, x1, x2;
if (x[0] < x[1]) { //*x[] is ascending
for (i = 0; i < n; i++) {
if (x0 <= x[i]) {
break;
}
}
}
else { //*x[] is descending
for (i = 0; i < n; i++) {
if (x0 >= x[i]) {
break;
}
}
}
if (i >= n - 1) {
i = n - 2;
}
else if (i <= 1) {
i = 0;
}
else {
i--;
}
x1 = x[i];
x2 = x[i + 1];
if (x1 == x2) {
return y[i];
}
y0 = y[i] + (x0 - x1) / (x2 - x1) * (y[i + 1] - y[i]);
return y0;
}
public static double insert3(double x0, double x[], double y[], int n) { //三点插值(double)
int i;
double y0, x1, x2, x3;
if (x[0] < x[1]) { //x[] is ascending
for (i = 0; i < n; i++) {
if (x0 <= x[i]) {
break;
}
}
}
else { //x[] is descending
for (i = 0; i < n; i++) {
if (x0 >= x[i]) {
break;
}
}
}
if (i >= n - 2) {
i = n - 3;
}
else if (i <= 1) {
i = 0;
}
else if ( (x0 - x[i - 1]) < (x[i] - x0)) {
i -= 2;
}
else {
i -= 1;
}
x1 = x[i];
x2 = x[i + 1];
x3 = x[i + 2];
if (x1 == x2 && x2 != x3) {
return (y[i + 2] - y[i + 1]) / (x3 - x2) * (x0 - x2) + y[i + 1];
}
if (x1 != x2 && x2 == x3) {
return (y[i + 1] - y[i]) / (x2 - x1) * (x0 - x1) + y[i];
}
if (x1 != x2 && x2 == x3) {
return y[i];
}
y0 = (x0 - x2) * (x0 - x3) / ( (x1 - x2) * (x1 - x3)) * y[i];
y0 += (x0 - x1) * (x0 - x3) / ( (x2 - x1) * (x2 - x3)) * y[i + 1];
y0 += (x0 - x1) * (x0 - x2) / ( (x3 - x1) * (x3 - x2)) * y[i + 2];
return y0;
}
public static float insert2(float x0, float x[], float y[], int n) { //两点插值(float)
if (n <= 1) {
return 0;
}
int i;
float y0, x1, x2;
if (x[0] < x[1]) { //*x[] is ascending
for (i = 0; i < n; i++) {
if (x0 <= x[i]) {
break;
}
}
}
else { //*x[] is descending
for (i = 0; i < n; i++) {
if (x0 >= x[i]) {
break;
}
}
}
if (i >= n - 1) {
i = n - 2;
}
else if (i <= 1) {
i = 0;
}
else {
i--;
}
x1 = x[i];
x2 = x[i + 1];
if (x1 == x2) {
return y[i];
}
y0 = y[i] + (x0 - x1) / (x2 - x1) * (y[i + 1] - y[i]);
return y0;
}
public static float insert3(float x0, float x[], float y[], int n) { //三点插值(float)
int i;
float y0, x1, x2, x3;
if (x[0] < x[1]) { //x[] is ascending
for (i = 0; i < n; i++) {
if (x0 <= x[i]) {
break;
}
}
}
else { //x[] is descending
for (i = 0; i < n; i++) {
if (x0 >= x[i]) {
break;
}
}
}
if (i >= n - 2) {
i = n - 3;
}
else if (i <= 1) {
i = 0;
}
else if ( (x0 - x[i - 1]) < (x[i] - x0)) {
i -= 2;
}
else {
i -= 1;
}
x1 = x[i];
x2 = x[i + 1];
x3 = x[i + 2];
if (x1 == x2 && x2 != x3) {
return (y[i + 2] - y[i + 1]) / (x3 - x2) * (x0 - x2) + y[i + 1];
}
if (x1 != x2 && x2 == x3) {
return (y[i + 1] - y[i]) / (x2 - x1) * (x0 - x1) + y[i];
}
if (x1 != x2 && x2 == x3) {
return y[i];
}
y0 = (x0 - x2) * (x0 - x3) / ( (x1 - x2) * (x1 - x3)) * y[i];
y0 += (x0 - x1) * (x0 - x3) / ( (x2 - x1) * (x2 - x3)) * y[i + 1];
y0 += (x0 - x1) * (x0 - x2) / ( (x3 - x1) * (x3 - x2)) * y[i + 2];
return y0;
}
public static float insert_zj2(float x, float c[], float d[]) {
float t = 0;
for (int i = 0; i < c.length - 1; i++) {
if ( (x > c[i]) & (x < c[i + 1])) {
t = ( ( (x - c[i]) / (c[i + 1] - c[i])) * (d[i + 1] - d[i])) +
d[i];
}
else if ( (x < c[i]) & (x > c[i + 1])) {
t = ( ( (x - c[i]) / (c[i + 1] - c[i])) * (d[i + 1] - d[i])) +
d[i];
}
else if (x == c[i]) {
t = d[i];
}
}
return t;
}
public static double insert_zj2(double x, double c[], double d[]) {
double t = 0;
for (int i = 0; i < c.length - 1; i++) {
if ( (x > c[i]) & (x < c[i + 1])) {
t = ( ( (x - c[i]) / (c[i + 1] - c[i])) * (d[i + 1] - d[i])) +
d[i];
}
else if ( (x < c[i]) & (x > c[i + 1])) {
t = ( ( (x - c[i]) / (c[i + 1] - c[i])) * (d[i + 1] - d[i])) +
d[i];
}
else if (x == c[i]) {
t = d[i];
}
}
return t;
}
public static int mod(int a, int b) { //取模
if (b == 0) {
return -1;
}
int c = a / b;
int left = a - c * b;
return left;
}
public static float maxValue(float[] q, int n) {
if (q == null) {
return 0;
}
float max = 0;
for (int i = 0; i < n; i++) {
if (q[i] > max) {
max = q[i];
}
}
return max;
}
public static int maxValue(int[] q, int n) {
if (q == null) {
return 0;
}
int max = 0;
for (int i = 0; i < n; i++) {
if (q[i] > max) {
max = q[i];
}
}
return max;
}
public static float minValue(float[] q, int n) {
if (q == null) {
return 0;
}
float min = 900000000;
for (int i = 0; i < n; i++) {
if (q[i] < min) {
min = q[i];
}
}
return min;
}
public static float sumValue(float[] q, int n) {
if (q == null) {
return 0;
}
float sum = 0f;
for (int i = 0; i < n; i++) {
sum += q[i];
}
return sum;
}
public static float average(float[] q, int n) {
if (q == null) {
return 0;
}
float sum = 0f;
for (int i = 0; i < n; i++) {
sum += q[i];
}
float average = sum / n;
return average;
}
public static double average(double[] q, int n) {
if (q == null) {
return 0;
}
double sum = 0f;
for (int i = 0; i < n; i++) {
sum += q[i];
}
double average = sum / n;
return average;
}
public static float Variance(float[] q, int n) {
if (q == null) {
return 0;
}
float sum = 0f, sum1 = 0f;
for (int i = 0; i < n; i++) {
sum += q[i];
}
float average = sum / n;
for (int j = 0; j < n; j++) {
sum1 = sum1 + (q[j] - average) * (q[j] - average);
}
float variance = (float) Math.sqrt( (double) sum1);
return variance;
}
public static double insertTwo(double x0, double[] x, double[] y) {
int n = x.length;
if (n <= 1) {
return -10000;
}
int i;
double y0, x1, x2;
if (x[0] < x[1]) { //*x[] is ascending
for (i = 0; i < n; i++) {
if (x0 <= x[i]) {
break;
}
}
}
else { //*x[] is descending
for (i = 0; i < n; i++) {
if (x0 >= x[i]) {
break;
}
}
}
if (i >= n - 1) {
i = n - 2;
}
else if (i <= 1) {
i = 0;
}
else {
i--;
}
x1 = x[i];
x2 = x[i + 1];
if (x1 == x2) {
return y[i];
}
else {
return y0 = y[i] + (x0 - x1) / (x2 - x1) * (y[i + 1] - y[i]);
}
}
public static double getMin(double[] dou) {
double tem = dou[0];
for (int i = 0; i < dou.length; i++) {
if (tem
- 1
- 2
前往页