Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
Last active August 29, 2015 14:13
Show Gist options
  • Save pgtwitter/0a6a17e7cc696e8eaa06 to your computer and use it in GitHub Desktop.
Save pgtwitter/0a6a17e7cc696e8eaa06 to your computer and use it in GitHub Desktop.
package rJava;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import org.rosuda.JRI.REXP;
public class RSample extends RBase implements KeyListener {
static int L = 30;
static int L2 = L * L;
static int BW = 5;
private int[] _boxes;
public static void main(String[] args) {
show(new RSample());
}
void init() {
_boxes = new int[L2];
int n = 80 * 30 * 30;
for (int i = 0; i < L2; i++) {
_boxes[i] = 0;
}
for (int i = 0; i < n; i++) {
_boxes[random(L2)]++;
}
_engine.assign("d", _boxes);
eval("d0<-d");
}
void plot() {
_engine.assign("d", _boxes);
eval("dmax<-max(d)");
eval("dmin<-min(d)");
eval(String.format("breaks<-seq(0,dmax+%d,by=%d)", BW, BW));
String args = "";
args += String.format(", xlim=c(0,dmax+%d)", BW);
args += ", breaks=breaks";
args += ", freq=TRUE";
args += String.format(", xlab='コマ内の点の数(階級[巾%d])'", BW);
args += ", ylab='コマの数(階級に属するコマの数)'";
args += ", family='Osaka-Mono'";
args += ", main=''";
eval(String.format("hist(d, col='#22222240', %s)", args));
eval("hist(d0, add=TRUE, ann=F, col='#CCCCCC40', breaks=breaks)");
eval("grid()");
eval("rug(d)");
eval(String.format("x<-breaks[1:length(breaks)-1]+%d/2", BW));
eval("y<-table(cut(d, breaks=breaks))");
curve_fitting();
}
void step() {
int n = 100000;
for (int i = 0; i < n; i++) {
if (!stepIMP()) {
i--;
continue;
}
}
}
// ------------------------------------------------
protected boolean stepIMP() {
int p = random(L2);
int q = random(L2);
if (_boxes[p] == 0 || p == q) {
return false;
}
_boxes[p]--;
_boxes[q]++;
return true;
}
private void curve_fitting() {
String f = "a*b^x";
String c = "solid";
REXP r = eval("result<-nls(y~(" + f + "), start=c(a=1, b=1))");
if (r == null)
return;
eval("p<-result$m$getPars()");
eval("a= p[1]");
eval("b= p[2]");
eval("func= function(x) " + f);
eval("plot(func, 0, dmax, add=TRUE, lty='" + c + "')");
String legend = "legend('topright'";
legend += String.format(", c(expression(%s))", f);
legend += String.format(", lty=c('%s')", c);
// legend += ", pch=c(-1)";
legend += ", bty='n')";
eval(legend);
}
// ------------------------------------------------
public RSample() {
super();
setFocusable(true); // KeyListnerに必要な設定
addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
toggle();
} else if (e.getKeyCode() == KeyEvent.VK_R) {
reset();
} else if (e.getKeyCode() == KeyEvent.VK_Q) {
quit();
}
}
// ------------------------------------------------
private static int random(final int n) {
return (int) Math.floor(Math.random() * n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment