Skip to content

Instantly share code, notes, and snippets.

@pif
Created November 9, 2010 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pif/669989 to your computer and use it in GitHub Desktop.
Save pif/669989 to your computer and use it in GitHub Desktop.
habe to impreve df
/*
При фиксированном значении k биномиальные коэффициенты могут быть вычислены по рекуррентной формуле
(n k) = n/(n-k) * (n-1 k)
с начальным значением (k k) = 1. Для вычисления значения этот метод требует O(1) памяти и O(n) времени.
*/
package ua.org.pifostap;
import java.io.*;
import java.util.Scanner;
public class Chyselni3 {
private static double f(double x) {
return Math.pow(Math.E, -x) * x * x;
}
private static double calcT(double x, double x0, double h) {
return (x - x0) / h;
}
private static double Ln(double x, int n, double a, double b) {
if (a >= b)
throw new IllegalArgumentException();
double h = Math.abs(a - b) / n;
double[] xi = new double[n + 1];
for (int i = 0; i < xi.length; ++i) {
xi[i] = a + i * h;
}
double t = calcT(x, a, h);
double res = 0;
double pre = 1;
for (int i = 0; i < n; ++i) {
res += pre * df(i, xi);
pre *= (t - i) / (i + 1);
}
return res;
}
private static double df(int n, double[] xi) {
double df = 0;
for (int i = 0; i <= n; ++i) {
df += Math.pow(-1, n - i) * c(i, n) * f(xi[i]);
}
//System.out.println("Df " + n + " equals " + df);
return df;
}
private static double c(int i, int n) {
int chys = 1;
for (int j = 1; j <= n; j++)
chys *= j;
int zn = 1;
for (int j = 1; j <= i; j++)
zn *= j;
for (int j = 1; j <= n - i; j++)
zn *= j;
return chys * 1d / zn;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a,b,n,x");
double a = s.nextDouble();
double b = s.nextDouble();
int n = s.nextInt();
double x = s.nextDouble();
System.out.println(" f(x): " + f(x));
System.out.println("Ln(x): " + Ln(x, n, a, b));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment