Java tutorial
/* * Copyright (C) 2015-2016 Willi Ye <williye97@gmail.com> * * This file is part of Kernel Adiutor. * * Kernel Adiutor is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Kernel Adiutor is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Kernel Adiutor. If not, see <http://www.gnu.org/licenses/>. * */ package com.fgl.calc.tools; import android.app.ActivityManager; import android.app.ActivityManager.RunningAppProcessInfo; import android.app.NotificationManager; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.AssetManager; import android.content.res.Resources; import android.graphics.BitmapFactory; import android.os.Build; import android.provider.Settings; import android.support.v4.app.NotificationCompat; import android.support.v7.app.AlertDialog; import android.util.Log; import android.view.Gravity; import android.widget.RemoteViews; import android.widget.TextView; import android.widget.Toast; import android.preference.Preference; import android.support.v4.content.ContextCompat; import android.text.Html; import android.annotation.TargetApi; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import java.util.Random; import static java.lang.Math.*; import java.util.function.Function; import net.objecthunter.exp4j.ExpressionBuilder; import net.objecthunter.exp4j.Expression; public class Tools implements Constants { private static double[] lroots; private static double[] weight; private static double[][] lcoef; public static double integral(double precision, double min, double max, FunctionA function) { double area = 0; double modifier = 1; if (min > max) { double tempMin = min; min = max; max = tempMin; modifier = -1; } for (double i = min + precision; i < max; i += precision) { double dFromA = i - min; area += (precision / 2) * (function.f(min + dFromA) + function.f(min + dFromA - precision)); } return area; } public interface FunctionA { public double f(double x); } public static void DoAToast(String message, Context context) { Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG); TextView view = (TextView) toast.getView().findViewById(android.R.id.message); if (view != null) view.setGravity(Gravity.CENTER); toast.show(); } public static void SendBroadcast(String action, Context context) { final Intent NewIntent = new Intent(); NewIntent.setAction(action); context.sendBroadcast(NewIntent); } public static String readString(String name, String defaults, Context context) { return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).getString(name, defaults); } //https://rosettacode.org/wiki/Numerical_integration/Gauss-Legendre_Quadrature public static double legeInte(int N, String eq, double a, double b) { values(N); legeCoef(N); legeRoots(N); Expression ex = new ExpressionBuilder(eq).variables("x").build(); double c1 = (b - a) / 2, c2 = (b + a) / 2, sum = 0; for (int i = 0; i < N; i++) { ex.setVariable("x", c1 * lroots[i] + c2); sum += weight[i] * ex.evaluate(); } return c1 * sum; } public static double legeInte2(int N, String eq, double xa, double xb, double ya, double yb) { values(N); legeCoef(N); legeRoots(N); Expression ex = new ExpressionBuilder(eq).variables("x", "y").build(); double cx1 = (xb - xa) / 2, cx2 = (xb + xa) / 2, sumx = 0; double cy1 = (yb - ya) / 2, cy2 = (yb + ya) / 2, sumy = 0; for (int i = 0; i < N; i++) { ex.setVariable("x", cx1 * lroots[i] + cx2); for (int j = 0; j < N; j++) { ex.setVariable("y", cy1 * lroots[j] + cy2); sumy += weight[i] * weight[j] * ex.evaluate(); } } return cx1 * cy1 * sumy; } public static void values(int N) { lroots = new double[N]; weight = new double[N]; lcoef = new double[N + 1][N + 1]; } public static void legeRoots(int N) { double x, x1; for (int i = 1; i <= N; i++) { x = cos(PI * (i - 0.25) / (N + 0.5)); do { x1 = x; x -= legeEval(N, x) / legeDiff(N, x); } while (x != x1); lroots[i - 1] = x; x1 = legeDiff(N, x); weight[i - 1] = 2 / ((1 - x * x) * x1 * x1); } } public static double legeEval(int n, double x) { double s = lcoef[n][n]; for (int i = n; i > 0; i--) s = s * x + lcoef[n][i - 1]; return s; } public static double legeDiff(int n, double x) { return n * (x * legeEval(n, x) - legeEval(n - 1, x)) / (x * x - 1); } public static void legeCoef(int N) { lcoef[0][0] = lcoef[1][1] = 1; for (int n = 2; n <= N; n++) { lcoef[n][0] = -(n - 1) * lcoef[n - 2][0] / n; for (int i = 1; i <= n; i++) { lcoef[n][i] = ((2 * n - 1) * lcoef[n - 1][i - 1] - (n - 1) * lcoef[n - 2][i]) / n; } } } }