diff --git a/src/main/java/info/istlab/Zemi01/App.java b/src/main/java/info/istlab/Zemi01/App.java index f09e901..f67d7c4 100644 --- a/src/main/java/info/istlab/Zemi01/App.java +++ b/src/main/java/info/istlab/Zemi01/App.java @@ -4,19 +4,16 @@ * 最初に実行されるクラス * */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "info.istlab.Zemi01.App.main()が実行されました。" ); - +public class App { + public static void main(String[] args) { + System.out.println("info.istlab.Zemi01.App.main()が実行されました。"); // 個別に、起動するクラスを指定する場合 // RectangleFrame.main(null); // ランチャーを起動する場合 - String[] opts = {"RectangleFrame", "TransparentClock"}; - Launcher.show(opts); + String[] opts = { "RectangleFrame", "TransparentClock", "NumericDemo" }; + Launcher.show(opts, null); } } diff --git a/src/main/java/info/istlab/Zemi01/Launcher.java b/src/main/java/info/istlab/Zemi01/Launcher.java index 6622633..babb1dd 100644 --- a/src/main/java/info/istlab/Zemi01/Launcher.java +++ b/src/main/java/info/istlab/Zemi01/Launcher.java @@ -16,12 +16,13 @@ public class Launcher extends JPanel implements ActionListener { String[] options; public static JFrame frame; + private String packageName; - public static void show(String[] opts) { + public static void show(String[] opts, String packName) { JFrame jf = frame = new JFrame("Launcher"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - jf.getContentPane().add(new Launcher(opts)); + jf.getContentPane().add(new Launcher(opts, packName)); jf.pack(); // 内容物でサイズを自動設定する Dimension winSize = jf.getSize(); @@ -35,8 +36,9 @@ return new Point((screenSize.width - winSize.width) / 2, (screenSize.height - winSize.height) / 2); } - public Launcher(String[] opts) { + public Launcher(String[] opts, String packName) { options = opts; + packageName = packName; setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); @@ -49,14 +51,19 @@ @Override public void actionPerformed(ActionEvent e) { - String clsname = e.getActionCommand(); //押されたボタンのラベル文字列 + String clsname = e.getActionCommand(); // 押されたボタンのラベル文字列 // 無駄にリフレクションAPIをつかって、クラス名からmainメソッドを起動してみる try { - // クラス名はパッケージ名を含めた名前が必要なので、このクラスのパッケージ名を取得する - String myClassName = Launcher.class.getPackageName(); - System.out.println("クラス "+myClassName + "." + clsname +" のmainメソッドを呼びます。"); - Class targetcls = Class.forName(myClassName + "." + clsname); + String packname; + if (packageName != null) + packname = packageName; + else { + // クラス名はパッケージ名を含めた名前が必要なので、このクラスのパッケージ名を取得する + packname = Launcher.class.getPackageName(); + } + System.out.println("クラス " + packname + "." + clsname + " のmainメソッドを呼びます。"); + Class targetcls = Class.forName(packname + "." + clsname); Class[] aarg = new Class[1]; aarg[0] = String[].class; // 文字列の配列。探したいメソッドは main(String[] args)なので。 diff --git a/src/main/java/info/istlab/Zemi01/NumericDemo.java b/src/main/java/info/istlab/Zemi01/NumericDemo.java new file mode 100644 index 0000000..f87aa4b --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/NumericDemo.java @@ -0,0 +1,9 @@ +package info.istlab.Zemi01; + +public class NumericDemo { + public static void main(String[] args) { + // ランチャーを起動する場合 + String[] opts = { "IEEE754view", "Lagrange", "RegLine" }; + Launcher.show(opts, "info.istlab.Zemi01.numeric"); + } +} diff --git a/src/main/java/info/istlab/Zemi01/Ttest.java b/src/main/java/info/istlab/Zemi01/Ttest.java deleted file mode 100644 index 682a4b4..0000000 --- a/src/main/java/info/istlab/Zemi01/Ttest.java +++ /dev/null @@ -1,309 +0,0 @@ -package info.istlab.Zemi01; - -import org.apache.commons.math3.distribution.TDistribution; - -public class Ttest { - public static void main(String[] args) { - double[] d1 = { 321, 269, 341, 318, 297, 308, 343, 337, 329 }; - double[] d2 = { 303, 253, 283, 333, 307, 310, 296, 265, 271 }; - double t1, t2; - System.out.println(t1 = paired_t(d1, d2)); - double p1 = tdist(t1, d1.length - 1, 2); - System.out.println("p1 = "+p1); - - System.out.println("----"); - - System.out.println("t2 = "+ (t2 = welch_t(d1, d2))); - double df2 = welch_degree_of_freedom(d1, d2); - System.out.println("df2 = " + df2); - double p2 = tdist(t2, df2, 2); - System.out.println("p2 = "+p2); - - // System.out.println("----"); - // TDistribution dist = new TDistribution(2); - // for(float x = -5; x < 5; x+=0.1){ - // System.out.println(x+" "+dist.cumulativeProbability(x)); - // } - } - - /** - * 合計 - */ - public static double sum(double[] ary) { - double tmp = 0; - for (int i = 0; i < ary.length; i++) { - tmp += ary[i]; - } - return tmp; - } - - /** - * 平均 - */ - public static double avg(double[] ary) { - double sum = sum(ary); - return sum / ary.length; - } - - /** - * 偏差:平均値との差の2乗 - */ - public static double hensa(double[] ary) { - double avg = avg(ary); - double tmp = 0; - for (int i = 0; i < ary.length; i++) { - tmp += (ary[i] - avg) * (ary[i] - avg); - } - return tmp; - } - - /** - * 不偏分散: 偏差を、データ数n-1 で割ったもの vars - */ - public static double var(double[] ary) { - double hensa = hensa(ary); - return hensa / (ary.length - 1); - } - - /** - * 標本分散: 偏差を、データ数n で割ったもの - */ - public static double varp(double[] ary) { - double hensa = hensa(ary); - return hensa / (ary.length); - } - - /** - * 差の配列 - * - * @param ary1 - * @param ary2 - * @return - */ - public static double[] diffary(double[] ary1, double[] ary2) { - double[] ret = new double[ary1.length]; - for (int i = 0; i < ary1.length; i++) { - ret[i] = ary1[i] - ary2[i]; - } - return ret; - } - - /** - * 対応のあるt検定の t値 - */ - public static double paired_t(double[] ary1, double[] ary2) { - // 差の平均値 / sqrt(差の不偏分散 / データ数) - double avg_sa = avg(diffary(ary1, ary2)); - double var_sa = var(diffary(ary1, ary2)); - return avg_sa / Math.sqrt(var_sa / ary1.length); - } - - /** - * 対応がない(なくてもよい) t検定 Welchのt - */ - public static double welch_t(double[] ary1, double[] ary2) { - // 不偏分散/データ数 - double stddev1 = var(ary1) / ary1.length; - double stddev2 = var(ary2) / ary2.length; - double diff_avg = avg(ary1) - avg(ary2); - return diff_avg / Math.sqrt(stddev1 + stddev2); - } - - public static double welch_degree_of_freedom(double[] ary1, double[] ary2) { - // 不偏分散/データ数 - double stddev1 = var(ary1) / ary1.length; - double stddev2 = var(ary2) / ary2.length; - double a1 = Math.pow(stddev1 + stddev2, 2); - double b1 = Math.pow(stddev1, 2) / (ary1.length - 1) + Math.pow(stddev2, 2) / (ary2.length - 1); - return a1 / b1; - } - - /** - * TDIST by commons-math3 TDistribution - * - * @param x - * @param df - * @param tails - * @return - */ - public static double tdist(double x, double deg_free, int tails) { - TDistribution tdistobj = new TDistribution(deg_free); - double cdf = tdistobj.cumulativeProbability(x); - double ret = (1-cdf) * tails; - // //test - // double test = tdist_old(x, deg_free, tails); - // if (test != ret) System.out.println("DIFF ret = "+ret+" test "+test ); - return ret; - } - - /** - * - * @param x - * @param deg_free - * @param tails - * @return - */ - public static double tdist_old(double x, double deg_free, int tails) { - return (1-studentsCdf(x, deg_free)) * tails; - } - - /** - * - * http://www.java2s.com/example/java-api/java/lang/math/exp-1-32.html - * - * - * Calculates the probability from -INF to X under Student's Distribution - * Ported to PHP from Javascript implementation found at - * http://www.math.ucla.edu/~tom/distributions/tDist.html - * // w w w .j av a 2 s. c o m - * - * @param x - * @param deg_free - * @return - */ - public static double studentsCdf(double x, double deg_free) { - if (deg_free <= 0) { - throw new IllegalArgumentException("The degrees of freedom need to be positive."); - } - - double A = deg_free / 2.0; - double S = A + 0.5; - double Z = deg_free / (deg_free + x * x); - double BT = Math.exp(logGamma(S) - logGamma(0.5) - logGamma(A) + A * Math.log(Z) + 0.5 * Math.log(1.0 - Z)); - double betacdf; - if (Z < (A + 1.0) / (S + 2.0)) { - betacdf = BT * betinc(Z, A, 0.5); - } else { - betacdf = 1 - BT * betinc(1.0 - Z, 0.5, A); - } - - double tcdf; - if (x < 0) { - tcdf = betacdf / 2.0; - } else { - tcdf = 1.0 - betacdf / 2.0; - } - return tcdf; - } - - /** - * Log Gamma Function - * - * @param Z - * @return - */ - public static double logGamma(double Z) { - double S = 1.0 + 76.18009173 / Z - 86.50532033 / (Z + 1.0) + 24.01409822 / (Z + 2.0) - - 1.231739516 / (Z + 3.0) + 0.00120858003 / (Z + 4.0) - 0.00000536382 / (Z + 5.0); - double LG = (Z - 0.5) * Math.log(Z + 4.5) - (Z + 4.5) + Math.log(S * 2.50662827465); - - return LG; - } - - /** - * Internal function used by StudentCdf - * - * @param x - * @param A - * @param B - * @return - */ - protected static double betinc(double x, double A, double B) { - double A0 = 0.0; - double B0 = 1.0; - double A1 = 1.0; - double B1 = 1.0; - double M9 = 0.0; - double A2 = 0.0; - while (Math.abs((A1 - A2) / A1) > 0.00000001) { - A2 = A1; - double C9 = -(A + M9) * (A + B + M9) * x / (A + 2.0 * M9) / (A + 2.0 * M9 + 1.0); - A0 = A1 + C9 * A0; - B0 = B1 + C9 * B0; - M9 = M9 + 1; - C9 = M9 * (B - M9) * x / (A + 2.0 * M9 - 1.0) / (A + 2.0 * M9); - A1 = A0 + C9 * A1; - B1 = B0 + C9 * B1; - A0 = A0 / B1; - B0 = B0 / B1; - A1 = A1 / B1; - B1 = 1.0; - } - return A1 / A; - } - - /** - * うまくいかないので使わない - * @param x - * @param v - * @return - */ - public static double studentsCdf2(double x, double v) { - double xL = x; - double xR = 100000; - int n = 10000; - double goukei = 0.0; - double a, b, xc, tmp; - double dx = (xR - xL) / n; - double B = simpson_betafunc(0.5, v / 2); - // double B = Beta.regularizedBeta(0.5, v / 2); - for (int i = 0; i < n; i++) { - a = xL + i * dx; - b = a + dx; - xc = a + dx / 2.0; - tmp = 0.0; - tmp = tmp + densityfunc(a, v, B) / 3; - tmp = tmp + densityfunc(xc, v, B) * 4 / 3; - tmp = tmp + densityfunc(b, v, B) / 3; - tmp = tmp * dx / 2; - goukei += tmp; - } - return goukei; - } - - private static double densityfunc(double x, double v, double B) { - return Math.pow(1.0 + (x * x / v), -(v + 1) / 2) / (Math.sqrt(v) * B); - } - - /** - * うまくいかないので使わない - * @param u - * @param v - * @return - */ - public static double simpson_betafunc(double u, double v) { - double xL = 0.0; - double xR = 1.0; - int n = 100; - double goukei = 0.0; - double a, b, xc, tmp; - double dx = (xR - xL) / n; - for (int i = 0; i < n; i++) { - a = xL + i * dx; - b = a + dx; - xc = a + dx / 2.0; - tmp = 0.0; - tmp = tmp + bfunc(a, u, v) / 3; - tmp = tmp + bfunc(xc, u, v) * 4 / 3; - tmp = tmp + bfunc(b, u, v) / 3; - tmp = tmp * dx / 2; - goukei += tmp; - System.out.println(i+" "+tmp+" "+goukei); - } - return goukei; - } - - /** - * うまくいかないので使わない - * @param x - * @param u - * @param v - * @return - */ - private static double bfunc(double x, double u, double v) { - double d = Math.pow(x, u - 1) * Math.pow(1.0 - x, v - 1); - System.out.println("bfunc "+x+" "+u+" "+v+" = "+d); - return d; - } - -} diff --git a/src/main/java/info/istlab/Zemi01/miuramath/CopyOfGraphPanel.java b/src/main/java/info/istlab/Zemi01/miuramath/CopyOfGraphPanel.java new file mode 100644 index 0000000..b4038ce --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/miuramath/CopyOfGraphPanel.java @@ -0,0 +1,237 @@ +package info.istlab.Zemi01.miuramath; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.geom.Point2D; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JPanel; + + +public class CopyOfGraphPanel extends JPanel implements MouseListener, MouseMotionListener { + + private static final long serialVersionUID = -919448575666487391L; + public static JFrame jf; + /** + * @param args + */ + public static void main(String[] args) { + jf = new JFrame("GraphPanel"); + jf.getContentPane().add(new CopyOfGraphPanel()); + jf.pack(); + jf.setVisible(true); + jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + Point2D offset; + + ArrayList points; + Dimension dim; + Point2D pointingPmath; + Point2D pressP; + int psize = 10; + int zoom = 20; + int gridgap = 1; + + Point2D cursorsnapCandidate; //カーソル位置のスナッピング + + public Dimension getPreferredSize(){ + return dim; + } + + public CopyOfGraphPanel(){ + offset = new Point2D.Double(0,0); + + points = new ArrayList(); + dim = new Dimension(500,400); + addMouseListener(this); + addMouseMotionListener(this); + points.add(new Point2D.Double(2,1)); + points.add(new Point2D.Double(5,7)); + setBackground(Color.white); + setForeground(Color.black); + } + public void paint(Graphics g){ + paintComponent(g); + } + public void paintComponent(Graphics g){ + Graphics2D g2d = (Graphics2D)g; + RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHints(rh); + + // g2d.setBackground(Color.white); + // g2d.setColor(Color.black); + g2d.clearRect(0,0,getWidth(),getHeight()); + + paintGrid(g2d); + + for(Point2D p:points){ + paintPoint(g2d, p); + // g2d.fillOval((int)p.getX()-psize/2, (int)p.getY()-psize/2, psize, psize); + // g2d.drawString(String.valueOf(num), (int)p.getX()+10, (int)p.getY()); + } + + if (pointingPmath != null){ + g2d.setColor(Color.red); + paintPoint(g2d, pointingPmath); + } + + // updateRegLine(); + paintLagrange(g2d); + + // g2d.setColor(Color.blue); + // g2d.drawString("a = "+String.valueOf(a), (int)(-getWidth()/2+10-offset.getX()), (int)(-getHeight()/2+20-offset.getY())); + // g2d.drawString("b = "+String.valueOf(b), (int)(-getWidth()/2+10-offset.getX()), (int)(-getHeight()/2+35-offset.getY())); + paintCursorSnap(g2d); + } + public void paintCursorSnap(Graphics2D g2d){ + if (cursorsnapCandidate != null){ + g2d.setColor(new Color(250,130,130)); + g2d.fillOval((int)(cursorsnapCandidate.getX()*zoom-psize/2), + (int)(-(cursorsnapCandidate.getY()*zoom+psize/2)), psize, psize); + } + } + + public void paintGrid(Graphics2D g2d){ + g2d.setColor(new Color(230,230,230)); + + g2d.translate(getWidth()/2+offset.getX(), getHeight()/2+offset.getY()); + // System.out.println(g2d.getTransform().getTranslateX()+" "+g2d.getTransform().getTranslateY()); + + for(int i=0;i2) + points.remove(pointing); + } + repaint(); + } + } + public void mouseEntered(MouseEvent e) { + } + public void mouseExited(MouseEvent e) { + } + public Point2D findPointing(Point2D mp){ + Point2D nearest = null; + Point2D mPmath = scr2math(mp); + for(Point2D p: points){ + if (mPmath.distance(p) < 1){ + nearest = p; + } + } + return nearest; + } + public void mousePressed(MouseEvent e) { + pressP = e.getPoint(); + Point2D pressPmath = snap(scr2math(pressP)); + Point2D nearest = null; + for(Point2D p: points){ + if (pressPmath.distance(p) < 1){ + nearest = p; + } + } + pointingPmath = nearest; + repaint(); + } + public void mouseReleased(MouseEvent e) { + pointingPmath = null; + repaint(); + } + public void mouseDragged(MouseEvent e) { + if (pointingPmath != null){ + pointingPmath.setLocation(snap(scr2math(e.getPoint()))); + cursorsnapCandidate = null; + repaint(); + } else { + // e.translatePoint((int)offset.getX(), (int)offset.getY()); + offset.setLocation(offset.getX()+(e.getPoint().getX()-pressP.getX()), + offset.getY()+(e.getPoint().getY()-pressP.getY())); + pressP = e.getPoint(); + repaint(); + } + } + public void mouseMoved(MouseEvent e) { + Point2D p = snap(scr2math(e.getPoint())); + cursorsnapCandidate = p; + System.out.println(p.toString()); + repaint(); + } + public Point2D snap(Point2D p){ + double pixel = gridgap; + double px = p.getX(); + double py = p.getY(); + System.out.println(px+" "+py); + return new Point2D.Double((int)(px/pixel)*pixel, (int)(py/pixel)*pixel); + } + // void logappend(String s){ + // jta.append(s+"\n"); + // jta.setCaretPosition(jta.getDocument().getLength()); + // } + +} diff --git a/src/main/java/info/istlab/Zemi01/miuramath/Function.java b/src/main/java/info/istlab/Zemi01/miuramath/Function.java new file mode 100644 index 0000000..137430b --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/miuramath/Function.java @@ -0,0 +1,25 @@ +package info.istlab.Zemi01.miuramath; + +import java.awt.Graphics2D; + +public class Function { + public double f(double x){ + return 1; + } + /** + * x の p乗を計算 + * @param x + * @param p + * @return + */ + public double pow(double x, int p){ + double ret = 1.0; + for(int i=0;i pts){ + N = pts.size(); + a = new double[N][N+1]; + vecX = new double[N]; + // パラメータの設定 + int i=0; + for(Point2D p: pts){ + + for(int j=0;j pts) { + double[] vecX = parameterByPoints(pts); + System.out.print("f ="); + for(int k=0; k max){ + max=Math.abs(a[i][k]); + ip=i; + } + } + } + if(ip != k){ + for(int j=k ; j<=N ; j++){ + double copy=a[k][j]; + a[k][j]=a[ip][j]; + a[ip][j]=copy; + } + } + } + + //前進消去 + public void forward(int N,int k,double a[][]){ + double p,q; + p=a[k][k]; + for(int j=k;j<=N;j++) + a[k][j]/=p; + if( k != N-1){ + for(int i=k+1;i=0;k--){ + double sum=0.0; + for(int j=k+1;j points; + Dimension dim; + Point2D pointingPmath; + Point2D pressP; + int psize = 10; + int zoom = 20; + int zoomy = 10; + int gridgap = 1; + + ArrayList funcs; + + Point2D cursorsnapCandidate; //カーソル位置のスナッピング + + PolyFunction polyfunc; + + public Dimension getPreferredSize(){ + return dim; + } + + public GraphPanel(){ + offset = new Point2D.Double(0,0); + + points = new ArrayList(); + funcs = new ArrayList(); + dim = new Dimension(500,900); + addMouseListener(this); + addMouseMotionListener(this); + addKeyListener(this); + addMouseWheelListener(this); + points.add(new Point2D.Double(-4,0)); + points.add(new Point2D.Double(-1,0)); + points.add(new Point2D.Double(0,-8)); + points.add(new Point2D.Double(2,0)); + setBackground(Color.white); + setForeground(Color.black); + setFocusable(true); + } + public void paint(Graphics g){ + paintComponent(g); + } + public void paintComponent(Graphics g){ + Graphics2D g2d = (Graphics2D)g; + RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHints(rh); + + // g2d.setBackground(Color.white); + // g2d.setColor(Color.black); + g2d.clearRect(0,0,getWidth(),getHeight()); + + paintGrid(g2d); + + g2d.setColor(Color.green); + for(Function f: funcs){ + paintFunc(g2d,f); + } + g2d.setColor(Color.black); + + + for(Point2D p:points){ + paintPoint(g2d, p); + // g2d.fillOval((int)p.getX()-psize/2, (int)p.getY()-psize/2, psize, psize); + // g2d.drawString(String.valueOf(num), (int)p.getX()+10, (int)p.getY()); + } + + if (pointingPmath != null){ + g2d.setColor(Color.red); + paintPoint(g2d, pointingPmath); + } + + // updateRegLine(); + paintLagrange(g2d); + + // g2d.setColor(Color.blue); + // g2d.drawString("a = "+String.valueOf(a), (int)(-getWidth()/2+10-offset.getX()), (int)(-getHeight()/2+20-offset.getY())); + // g2d.drawString("b = "+String.valueOf(b), (int)(-getWidth()/2+10-offset.getX()), (int)(-getHeight()/2+35-offset.getY())); + paintCursorSnap(g2d); + } + public void paintCursorSnap(Graphics2D g2d){ + if (cursorsnapCandidate != null){ + g2d.setColor(new Color(250,130,130)); + g2d.fillOval((int)(cursorsnapCandidate.getX()*zoom-psize/2), + (int)(-(cursorsnapCandidate.getY()*zoomy+psize/2)), psize, psize); + } + } + + public void paintGrid(Graphics2D g2d){ + g2d.setColor(new Color(230,230,230)); + + g2d.translate(getWidth()/2+offset.getX(), getHeight()/2+offset.getY()); + // System.out.println(g2d.getTransform().getTranslateX()+" "+g2d.getTransform().getTranslateY()); + + for(int i=0;i2) + points.remove(pointing); + } + samplingPointUpdated(); + repaint(); + } + } + public void mouseEntered(MouseEvent e) { + requestFocus(); + } + public void mouseExited(MouseEvent e) { + } + public Point2D findPointing(Point2D mp){ + Point2D nearest = null; + Point2D mPmath = scr2math(mp); + for(Point2D p: points){ + if (mPmath.distance(p) < gridgap){ + nearest = p; + } + } + return nearest; + } + public void mousePressed(MouseEvent e) { + pressP = e.getPoint(); + Point2D pressPmath = snap(scr2math(pressP)); + Point2D nearest = null; + for(Point2D p: points){ + if (pressPmath.distance(p) < gridgap){ + nearest = p; + } + } + pointingPmath = nearest; + repaint(); + } + public void mouseReleased(MouseEvent e) { + /*if (pointingPmath != null) */ samplingPointUpdated(); + pointingPmath = null; + repaint(); + } + public void samplingPointUpdated() { + + } + + public void mouseDragged(MouseEvent e) { + if (pointingPmath != null){ + pointingPmath.setLocation(snap(scr2math(e.getPoint()))); + cursorsnapCandidate = null; + repaint(); + } else { + // e.translatePoint((int)offset.getX(), (int)offset.getY()); + offset.setLocation(offset.getX()+(e.getPoint().getX()-pressP.getX()), + offset.getY()+(e.getPoint().getY()-pressP.getY())); + pressP = e.getPoint(); + repaint(); + } + } + public void mouseMoved(MouseEvent e) { + Point2D p = snap(scr2math(e.getPoint())); + cursorsnapCandidate = p; +// System.out.println(p.toString()); + repaint(); + } + public Point2D snap(Point2D p){ + double pixel = gridgap; + double px = p.getX(); + double py = p.getY(); +// System.out.println(px+" "+py); + return new Point2D.Double(Math.round(px*pixel/pixel), Math.round(py*pixel/pixel)); + } + // void logappend(String s){ + // jta.append(s+"\n"); + // jta.setCaretPosition(jta.getDocument().getLength()); + // } + + @Override + public void keyPressed(KeyEvent arg0) { + if (arg0.getKeyCode() == KeyEvent.VK_R){ + polyfunc.setIntParam(); + repaint(); + + } + + } + + @Override + public void keyReleased(KeyEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void keyTyped(KeyEvent arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseWheelMoved(MouseWheelEvent arg0) { + if (arg0.isControlDown()){ + gridgap += arg0.getWheelRotation(); + if (gridgap < 1) gridgap = 1; +// System.out.println("Grid Gap = "+gridgap); + } else { + zoomy += arg0.getWheelRotation(); + if (zoomy < 1) zoomy = 1; +// zoom = zoomy*2; +// System.out.println("Zoom = "+zoom); + } + repaint(); + } + +} diff --git a/src/main/java/info/istlab/Zemi01/miuramath/GraphPanel_ShowFunction.java b/src/main/java/info/istlab/Zemi01/miuramath/GraphPanel_ShowFunction.java new file mode 100644 index 0000000..9e85252 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/miuramath/GraphPanel_ShowFunction.java @@ -0,0 +1,113 @@ +package info.istlab.Zemi01.miuramath; + +import java.awt.Graphics; +import java.awt.event.MouseEvent; +import java.util.ArrayList; + +import javax.swing.JFrame; + + +public class GraphPanel_ShowFunction extends GraphPanel { + + private static final long serialVersionUID = -919448575666487391L; + public static JFrame jf; + /** + * @param args + */ + public static void main(String[] args) { + jf = new JFrame("ラグランジュ点から関数のパラメータを表示する"); + jf.getContentPane().add(new GraphPanel_ShowFunction()); + jf.pack(); + jf.setVisible(true); + jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } +//サンプリング点が動いたかも。 + public void samplingPointUpdated() { + if (polyfunc!=null) { + polyfunc.setSamplingPoints(points); + polyfunc.showSamplingPoints(); + } + show_func_info(); + } + + + public GraphPanel_ShowFunction(){ + super(); + + /** PolyFunctionの配列について + * 先頭は p そのあとは a,b,c,d,e,... で,意味は + * p(x-a)(x-b)(x-c)(x-d)... + e + * {p,a,b,c,d,e} + * length=6 なら4次 5なら3次 + */ +// funcs.add(polyfunc = new PolyFunction(new double[]{-1, -2,1,2,1})); + + // 3次の式のとき +// funcs.add(polyfunc = new PolyFunction(new double[]{1.0, -1,1,2,3})); + + // 4次の式のとき +// funcs.add(polyfunc = new PolyFunction(new double[]{1.0, -1,1,2,3,-1})); +// funcs.add(polyfunc = new PolyFunction(new double[]{-0.5, -5,-3,-1,2,7})); // 2017SAISI +// funcs.add(polyfunc = new PolyFunction(new double[]{-1, -7,-3,-1, 5 })); // 2018-ken +// funcs.add(polyfunc = new PolyFunction(new double[]{ 1, -5,-1, 2, -4 })); // 2018-mate +// funcs.add(polyfunc = new PolyFunction(new double[]{ 0.5, -3, 2, 6, -8 })); // 2018-kikai +// function_changed(); + +// funcs.add(polyfunc = new PolyFunction(new double[][]{{-6,-10},{-4,-4},{-2,10},{-1,5}})); // 2018-ken1 検算 +// funcs.add(polyfunc = new PolyFunction(new double[][]{{-5,-4},{-4,14},{-3,16},{-2,8}})); // 2018-mate1検算 + funcs.add(polyfunc = new PolyFunction(new double[][]{{-2,8},{-1,13},{1,2},{3,-17}})); // 2018-kikai1検算 + +// (-2 , 8) , (-1 , 13) , (1 , 2) , (3 , -17) + show_func_info(); + polyfunc.editor(this); + } + public void function_changed(){ + points.clear(); + points.addAll(polyfunc.getIntPoints()); + System.out.println("------"); + polyfunc.printParams();// 当初の指定パラメータを出力 + updateFunc(); + show_func_info(); + } + public void show_func_info() { + polyfunc.print(); + System.out.print("d"); + polyfunc.diff().print(); + polyfunc.printIntPoints(); + ArrayList results = Newton.solve(polyfunc); + System.out.println("f(x)の解 "+results.toString()); + ArrayList simpsons = Simpson.allIntegral(polyfunc,results); + System.out.println("f(x)の、解の範囲での積分値: "+simpsons.toString()); + + System.out.println("===bibun==="); + ArrayList res2 = Newton.solve(polyfunc.diff()); + System.out.println("df(x)の解 "+res2.toString()); + + repaint(); + } + public void paint(Graphics g){ + super.paintComponent(g); + } + public void paintComponent(Graphics g){ + super.paintComponent(g); + + } + public void mouseClicked(MouseEvent e){ + super.mouseClicked(e); + updateFunc(); + } + + public void mouseReleased(MouseEvent e) { + super.mouseReleased(e); + updateFunc(); + } + public void mouseDragged(MouseEvent e) { + super.mouseDragged(e); + updateFunc(); + } + public void updateFunc(){ + Gauss g = new Gauss(); + g.parameterByPoints(points); + } + +} diff --git a/src/main/java/info/istlab/Zemi01/miuramath/Newton.java b/src/main/java/info/istlab/Zemi01/miuramath/Newton.java new file mode 100644 index 0000000..b0e7e95 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/miuramath/Newton.java @@ -0,0 +1,45 @@ +package info.istlab.Zemi01.miuramath; + +import java.util.ArrayList; + +public class Newton { + public static ArrayList solve(PolyFunction func){ + ArrayList results = new ArrayList(); +// PolyFunction diff = func.diff(); +// diff.print(); + + double fx = func.f(-10); + Newton newton = new Newton(); + for(double x = -10; x<10;x +=0.1){ + double nfx = func.f(x); + if (fx*nfx<0) { +// System.out.println("changed at" +x); + // 符号が変わった! + double ans = newton.ans(func, x); +// System.out.println(ans ); + results.add(ans); + } + fx = nfx; + } + return results; + } + + public Newton(){ + + } + + public double ans(PolyFunction func, double initx){ + PolyFunction diff = func.diff(); +// diff.print(); + double x = initx; + + while(true){ + double newx = x - (func.f(x)/diff.f(x)); +// System.out.println("newx "+newx+" "); + if (Math.abs(newx-x)<0.00000000001){ + return newx; + } + x = newx; + } + } +} diff --git a/src/main/java/info/istlab/Zemi01/miuramath/PolyFunction.java b/src/main/java/info/istlab/Zemi01/miuramath/PolyFunction.java new file mode 100644 index 0000000..d2630f6 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/miuramath/PolyFunction.java @@ -0,0 +1,227 @@ +package info.istlab.Zemi01.miuramath; + +import java.awt.Dimension; +import java.awt.Graphics2D; +import java.awt.geom.Point2D; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.SpinnerNumberModel; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +public class PolyFunction extends Function implements ChangeListener { + double a[]; + double onx[] = {1.0, -3, -1, 2, 5 }; +//点列データで与える場合 + ArrayList spts; + + JSpinner sp[]; + SpinnerNumberModel snm[]; + GraphPanel_ShowFunction gpanel; + + //点列データで作成する場合 + public PolyFunction(double spt[][]) { + spts = new ArrayList(); + int len = spt.length; + for(int i=0;i sp) { + setSamplingPoints(sp); + } + public void setSamplingPoints(ArrayList sp) { + spts = sp; + updateParamsFromSamplingPoints(sp); //update a[] + } + private void updateParamsFromSamplingPoints(ArrayList sp2) { + Gauss g = new Gauss(); + double[] vx = g.parameterByPoints(sp2); + //反転させる + int N = sp2.size(); + a = new double[N]; + for(int i=0;i0;i--){ + b[i-1] = i*a[i]; + } + return new PolyFunction(b,true); + } + + public PolyFunction(double[] ox){ + onx = ox; + updateParamsFromXAXIS(ox); +// onx = ox; +// int N = onx.length-1; +// a = new double[N]; +// a[N-1] = onx[0]; +// a[N-2] = -(onx[1]+onx[2]+onx[3]) * onx[0]; +// a[N-3] = (onx[1]*onx[2] + onx[2]*onx[3] + onx[3]*onx[1]) * onx[0]; +// a[N-4] = -(onx[0]*onx[1]*onx[2]*onx[3]) + onx[4]; +// // a[N-4] = (-(onx[1]*onx[2]*onx[3]) + onx[4]) * onx[0]; + } + + /** ox の先頭は p + * p(x-a)(x-b)(x-c)(x-d)... + e + * {p,a,b,c,d,e} + * length=6 なら4次 5なら3次 + */ + public void updateParamsFromXAXIS(double[] ox){ + double mul = pow(-1.0, ox.length-2); //-1 for 3次,1 for 4次 etc... + for(int i=0;i getIntPoints() { + ArrayList pts = new ArrayList(); + for(int i=1;i0.0000000001) continue; + if (Math.abs(f(x))< 100) + System.out.print("("+x+" , "+Math.round(f(x))+") , "); + } + System.out.println(""); + } + public void print(){ + // System.out.println(""); + System.out.print("f ="); + int N = a.length; + for(int k=N-1; k>-1; k--){ + System.out.printf("%+7.2f ",a[k]); + if (k>0) System.out.print("*x"); + if (k>1) System.out.print("**"+k); + } + System.out.println(""); + } + public void printParams() { + for(int i=0;i allIntegral(PolyFunction func, + ArrayList regions) { + ArrayList results = new ArrayList(); + for(int i=0;i 0.00000001) { + A2 = A1; + double C9 = -(A + M9) * (A + B + M9) * x / (A + 2.0 * M9) / (A + 2.0 * M9 + 1.0); + A0 = A1 + C9 * A0; + B0 = B1 + C9 * B0; + M9 = M9 + 1; + C9 = M9 * (B - M9) * x / (A + 2.0 * M9 - 1.0) / (A + 2.0 * M9); + A1 = A0 + C9 * A1; + B1 = B0 + C9 * B1; + A0 = A0 / B1; + B0 = B0 / B1; + A1 = A1 / B1; + B1 = 1.0; + } + return A1 / A; + } + + /** + * うまくいかないので使わない + * @param x + * @param v + * @return + */ + public static double studentsCdf2(double x, double v) { + double xL = x; + double xR = 100000; + int n = 10000; + double goukei = 0.0; + double a, b, xc, tmp; + double dx = (xR - xL) / n; + double B = simpson_betafunc(0.5, v / 2); + // double B = Beta.regularizedBeta(0.5, v / 2); + for (int i = 0; i < n; i++) { + a = xL + i * dx; + b = a + dx; + xc = a + dx / 2.0; + tmp = 0.0; + tmp = tmp + densityfunc(a, v, B) / 3; + tmp = tmp + densityfunc(xc, v, B) * 4 / 3; + tmp = tmp + densityfunc(b, v, B) / 3; + tmp = tmp * dx / 2; + goukei += tmp; + } + return goukei; + } + + private static double densityfunc(double x, double v, double B) { + return Math.pow(1.0 + (x * x / v), -(v + 1) / 2) / (Math.sqrt(v) * B); + } + + /** + * うまくいかないので使わない + * @param u + * @param v + * @return + */ + public static double simpson_betafunc(double u, double v) { + double xL = 0.0; + double xR = 1.0; + int n = 100; + double goukei = 0.0; + double a, b, xc, tmp; + double dx = (xR - xL) / n; + for (int i = 0; i < n; i++) { + a = xL + i * dx; + b = a + dx; + xc = a + dx / 2.0; + tmp = 0.0; + tmp = tmp + bfunc(a, u, v) / 3; + tmp = tmp + bfunc(xc, u, v) * 4 / 3; + tmp = tmp + bfunc(b, u, v) / 3; + tmp = tmp * dx / 2; + goukei += tmp; + System.out.println(i+" "+tmp+" "+goukei); + } + return goukei; + } + + /** + * うまくいかないので使わない + * @param x + * @param u + * @param v + * @return + */ + private static double bfunc(double x, double u, double v) { + double d = Math.pow(x, u - 1) * Math.pow(1.0 - x, v - 1); + System.out.println("bfunc "+x+" "+u+" "+v+" = "+d); + return d; + } + +} diff --git a/src/main/java/info/istlab/Zemi01/miuramath/old__PolyFunction.java b/src/main/java/info/istlab/Zemi01/miuramath/old__PolyFunction.java new file mode 100644 index 0000000..505a2e2 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/miuramath/old__PolyFunction.java @@ -0,0 +1,64 @@ +package info.istlab.Zemi01.miuramath; + +import java.awt.Graphics2D; + +public class old__PolyFunction extends Function { + double a[]; + + public old__PolyFunction(int nanji){ + + setIntParam(nanji); + } + public old__PolyFunction(double[] para){ + a = para; + } + public old__PolyFunction diff(){ + int N = a.length; + double b[] = new double[N-1]; + for(int i=N-1;i>0;i--){ + b[i-1] = i*a[i]; + } + return new old__PolyFunction(b); + } + public double f(double x){ + int N = a.length; + double sum = 0; + for(int i=0;i-1; i--){ + sb.append(String.format("%10.3f ", a[i])); + } + g2d.drawString(sb.toString(), x, y); + } + public void print(){ + System.out.println(""); + System.out.print("f ="); + int N = a.length; + for(int k=a.length-1; k>-1; k--){ + System.out.printf(" %+5.0f ",a[k]); + if (k -1) { + try { + sb.insert(shift + 1, "."); + } catch (StringIndexOutOfBoundsException ex) { + sb.append(" "); + for (int i = 1; i <= shift - 23; i++) + sb.append("0"); + // sb.deleteCharAt(23); + + } + } else { + StringBuffer pre = new StringBuffer(); + for (int i = 1; i <= Math.abs(shift); i++) { + pre.append("0"); + } + pre.insert(1, "."); + sb.insert(0, pre.toString()); + } + return sb.toString(); + } + + void logappend(String s) { + jta.append(s + "\n"); + jta.setCaretPosition(jta.getDocument().getLength()); + } + + void update2to10(String s2) { + // System.out.println(s2); + String orig = s2.replaceAll(" ", "") + "000000000000000000000000000000000000000000000"; + // System.out.println(orig); + int p; + try { + p = Integer.parseInt(orig.substring(1, 32), 2); + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(this, "入力値は変換できません"); + return; + } + int cursorpos = jtf2.getCaretPosition(); + jtf2.setText(separated(orig.substring(0, 32))); + jtf2.setCaretPosition(cursorpos); + float f = Float.intBitsToFloat(p); + if (orig.charAt(0) == '1') + f = -f; + int sisuu = Integer.parseInt(orig.substring(1, 9), 2); + // System.out.println(f); + if (Float.NaN != f) { + jtf10.setText(String.valueOf(f)); + jtf16.setText(orig.substring(1, 9) + " = " + sisuu + " = 127 + ( " + (sisuu - 127) + + " )"); + jtf2real.setText(orig754to2(orig.substring(0, 32))); + + // jtf16.setText(Float.toHexString(f) + " / " + orig.substring(1, 9) + " = " + + // sisuu + " = " + (sisuu - 127) + // + " + 127"); + logappend(separated(orig.substring(0, 32)) + " ⇒ " + String.valueOf(f)); + } else { + JOptionPane.showMessageDialog(this, "入力値は変換できません"); + } + } + + // IEEE754表記の、符号と指数と仮数4bitずつの区切りスペースをいれる。入力文字列は区切り無し。 + public String separated(String s) { + StringBuffer sb = new StringBuffer(s); + sb.insert(29, " "); + sb.insert(25, " "); + sb.insert(21, " "); + sb.insert(17, " "); + sb.insert(13, " "); + sb.insert(9, " "); + sb.insert(1, " "); + + return sb.toString(); + } + + // 10進数を2進数表記文字列へ変換するメソッド + public static final String toBinaryDigit(int a) { + StringBuffer buf = new StringBuffer(); + for (int i = 31; i >= 0; i--) { // Javaのintは32ビット(4バイト) + buf.append((a & (int) Math.pow(2, i)) >>> i); + } + return buf.toString(); + } + + @Override + public void windowOpened(WindowEvent e) { + jtf10.requestFocus(); + + } + + @Override + public void windowClosing(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowClosed(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowIconified(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowDeiconified(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowActivated(WindowEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void windowDeactivated(WindowEvent e) { + // TODO Auto-generated method stub + + } +} \ No newline at end of file diff --git a/src/main/java/info/istlab/Zemi01/numeric/Lagrange.java b/src/main/java/info/istlab/Zemi01/numeric/Lagrange.java new file mode 100644 index 0000000..e383da5 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/numeric/Lagrange.java @@ -0,0 +1,223 @@ +package info.istlab.Zemi01.numeric; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.geom.Point2D; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +import info.istlab.Zemi01.Launcher; + + +public class Lagrange extends JPanel implements MouseListener, MouseMotionListener { + + private static final long serialVersionUID = -919448575666487391L; + public static JFrame jf; + /** + * @param args + */ + public static void main(String[] args) { + jf = new JFrame("Lagrange Interpolation"); + jf.getContentPane().add(new Lagrange()); + jf.pack(); + jf.setVisible(true); + jf.setLocation(Launcher.centerOfScreen(jf.getSize())); + + jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + System.out.println("ラグランジュ補間 Lagrange interpolation https://kyutech.istlab.info/ouyo/#!lec121.md "); + System.out.println("つかいかた:(1)右クリックで、点を追加 (2)左ドラッグで、点移動 or グラフ移動 (3) 点の上で右クリックすると、点が消えます。"); + } + Point2D offset; + + ArrayList points; + Dimension dim; + Point2D pointingPmath; + Point2D pressP; + int psize = 10; + + + public Dimension getPreferredSize(){ + return dim; + } + + public Lagrange(){ + offset = new Point2D.Double(0,0); + + points = new ArrayList(); + dim = new Dimension(500,400); + addMouseListener(this); + addMouseMotionListener(this); + points.add(new Point2D.Double(100,100)); + points.add(new Point2D.Double(50,70)); + setBackground(Color.white); + setForeground(Color.black); + } + public void paint(Graphics g){ + paintComponent(g); + } + public void paintComponent(Graphics g){ + Graphics2D g2d = (Graphics2D)g; + RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHints(rh); + + // g2d.setBackground(Color.white); + // g2d.setColor(Color.black); + g2d.clearRect(0,0,getWidth(),getHeight()); + + paintGrid(g2d); + + int num = 0; + for(Point2D p:points){ + paintPoint(g2d, p); + // g2d.fillOval((int)p.getX()-psize/2, (int)p.getY()-psize/2, psize, psize); + // g2d.drawString(String.valueOf(num), (int)p.getX()+10, (int)p.getY()); + num++; + } + + if (pointingPmath != null){ + g2d.setColor(Color.red); + paintPoint(g2d, pointingPmath); + } + + // updateRegLine(); + paintLagrange(g2d); + + // g2d.setColor(Color.blue); + // g2d.drawString("a = "+String.valueOf(a), (int)(-getWidth()/2+10-offset.getX()), (int)(-getHeight()/2+20-offset.getY())); + // g2d.drawString("b = "+String.valueOf(b), (int)(-getWidth()/2+10-offset.getX()), (int)(-getHeight()/2+35-offset.getY())); + } + + public void paintGrid(Graphics2D g2d){ + g2d.setColor(new Color(230,230,230)); + + g2d.translate(getWidth()/2+offset.getX(), getHeight()/2+offset.getY()); + // System.out.println(g2d.getTransform().getTranslateX()+" "+g2d.getTransform().getTranslateY()); + + for(int i=0;i2) + points.remove(pointing); + } + repaint(); + } + } + public void mouseEntered(MouseEvent e) { + } + public void mouseExited(MouseEvent e) { + } + public Point2D findPointing(Point2D mp){ + Point2D nearest = null; + Point2D mPmath = scr2math(mp); + for(Point2D p: points){ + if (mPmath.distance(p) < 8){ + nearest = p; + } + } + return nearest; + } + public void mousePressed(MouseEvent e) { + scr2math(e.getPoint()); + pressP = e.getPoint(); + Point2D pressPmath = scr2math(pressP); + Point2D nearest = null; + for(Point2D p: points){ + if (pressPmath.distance(p) < 8){ + nearest = p; + } + } + pointingPmath = nearest; + repaint(); + } + public void mouseReleased(MouseEvent e) { + pointingPmath = null; + repaint(); + } + public void mouseDragged(MouseEvent e) { + if (pointingPmath != null){ + pointingPmath.setLocation(scr2math(e.getPoint())); + repaint(); + } else { + // e.translatePoint((int)offset.getX(), (int)offset.getY()); + offset.setLocation(offset.getX()+(e.getPoint().getX()-pressP.getX()), + offset.getY()+(e.getPoint().getY()-pressP.getY())); + pressP = e.getPoint(); + repaint(); + } + } + public void mouseMoved(MouseEvent e) { + } + // void logappend(String s){ + // jta.append(s+"\n"); + // jta.setCaretPosition(jta.getDocument().getLength()); + // } + +} diff --git a/src/main/java/info/istlab/Zemi01/numeric/RegLine.java b/src/main/java/info/istlab/Zemi01/numeric/RegLine.java new file mode 100644 index 0000000..4038727 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/numeric/RegLine.java @@ -0,0 +1,232 @@ +package info.istlab.Zemi01.numeric; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.geom.Point2D; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +import info.istlab.Zemi01.Launcher; + + +public class RegLine extends JPanel implements MouseListener, MouseMotionListener { + + private static final long serialVersionUID = -919448575666487391L; + public static JFrame jf; + /** + * @param args + */ + public static void main(String[] args) { + jf = new JFrame("Regression Line"); + jf.getContentPane().add(new RegLine()); + jf.pack(); + jf.setVisible(true); + jf.setLocation(Launcher.centerOfScreen(jf.getSize())); + + jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + System.out.println("線形補間 Linear Interpolation 最小二乗法 https://kyutech.istlab.info/ouyo/#!lec12.md "); + System.out.println("つかいかた:(1)右クリックで、点を追加 (2)左ドラッグで、点移動 or グラフ移動 (3) 点の上で右クリックすると、点が消えます。"); + + } + Point2D offset; + + ArrayList points; + Dimension dim; + Point2D pointingPmath; + Point2D pressP; + int psize = 10; + + //回帰直線の係数 + double a,b; + + public Dimension getPreferredSize(){ + return dim; + } + public void updateRegLine(){ + //平均値をもとめる + double xavg=0, yavg=0; + double xsum=0, ysum=0; + for(Point2D p:points){ + xsum+=p.getX(); + ysum+=p.getY(); + } + xavg=xsum/points.size(); + yavg=ysum/points.size(); +// System.out.println("xavg "+xavg+" "+yavg); + + //aup,adownをもとめる + double xiyi = 0; + double xi2 = 0; + double aup,adown; + for(Point2D p:points){ + xiyi += (p.getX()*p.getY()); + xi2 += (p.getX()*p.getX()); + } + aup = xiyi - (points.size()*xavg*yavg); + adown = xi2 - (points.size()*xavg*xavg); + a = aup/adown; + + b = yavg - (a*xavg); +// b = ((xi2*ysum)-(xiyi*xsum))/(points.size()*xi2 - xsum*xsum); + } + + public RegLine(){ + offset = new Point2D.Double(0,0); + + points = new ArrayList(); + dim = new Dimension(500,400); + addMouseListener(this); + addMouseMotionListener(this); + points.add(new Point2D.Double(100,100)); + points.add(new Point2D.Double(50,70)); + setBackground(Color.white); + setForeground(Color.black); + } + public void paint(Graphics g){ + paintComponent(g); + } + public void paintComponent(Graphics g){ + Graphics2D g2d = (Graphics2D)g; + RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); + g2d.setRenderingHints(rh); + +// g2d.setBackground(Color.white); +// g2d.setColor(Color.black); + g2d.clearRect(0,0,getWidth(),getHeight()); + + paintGrid(g2d); + + int num = 0; + for(Point2D p:points){ + paintPoint(g2d, p); + // g2d.fillOval((int)p.getX()-psize/2, (int)p.getY()-psize/2, psize, psize); +// g2d.drawString(String.valueOf(num), (int)p.getX()+10, (int)p.getY()); + num++; + } + + if (pointingPmath != null){ + g2d.setColor(Color.red); + paintPoint(g2d, pointingPmath); + } + + updateRegLine(); + paintRegLine(g2d); + + g2d.setColor(Color.blue); + g2d.drawString("a = "+String.valueOf(a), (int)(-getWidth()/2+10-offset.getX()), (int)(-getHeight()/2+20-offset.getY())); + g2d.drawString("b = "+String.valueOf(b), (int)(-getWidth()/2+10-offset.getX()), (int)(-getHeight()/2+35-offset.getY())); + } + + public void paintGrid(Graphics2D g2d){ + g2d.setColor(new Color(230,230,230)); + + g2d.translate(getWidth()/2+offset.getX(), getHeight()/2+offset.getY()); +// System.out.println(g2d.getTransform().getTranslateX()+" "+g2d.getTransform().getTranslateY()); + + for(int i=0;i2) + points.remove(pointing); + } + repaint(); + } + } + public void mouseEntered(MouseEvent e) { + } + public void mouseExited(MouseEvent e) { + } + public Point2D findPointing(Point2D mp){ + Point2D nearest = null; + Point2D mPmath = scr2math(mp); + for(Point2D p: points){ + if (mPmath.distance(p) < 8){ + nearest = p; + } + } + return nearest; + } + public void mousePressed(MouseEvent e) { + scr2math(e.getPoint()); + pressP = e.getPoint(); + Point2D pressPmath = scr2math(pressP); + Point2D nearest = null; + for(Point2D p: points){ + if (pressPmath.distance(p) < 8){ + nearest = p; + } + } + pointingPmath = nearest; + repaint(); + } + public void mouseReleased(MouseEvent e) { + pointingPmath = null; + repaint(); + } + public void mouseDragged(MouseEvent e) { + if (pointingPmath != null){ + pointingPmath.setLocation(scr2math(e.getPoint())); + repaint(); + } else { +// e.translatePoint((int)offset.getX(), (int)offset.getY()); + offset.setLocation(offset.getX()+(e.getPoint().getX()-pressP.getX()), + offset.getY()+(e.getPoint().getY()-pressP.getY())); + pressP = e.getPoint(); + repaint(); + } + } + public void mouseMoved(MouseEvent e) { + } +// void logappend(String s){ +// jta.append(s+"\n"); +// jta.setCaretPosition(jta.getDocument().getLength()); +// } + +}