From 70d45b57ef59585bb8b82123d054014255d260ca Mon Sep 17 00:00:00 2001 From: HOSOKAWA Kenchi Date: Sun, 22 Aug 2010 11:47:31 +0900 Subject: [PATCH] 323 --- dev4/psychlops/core/graphic/canvas.cs | 18 ++--- dev4/psychlops/core/math/interval.cs | 86 ++++++++++++++-------- dev4/psychlops/core/math/matrix.cs | 66 ++++++++++++----- dev4/psychlops/core/math/util.cs | 45 ++++++++--- .../extention/compatibility/compatibility.cs | 41 +++++++++++ dev4/psychlops/extention/math/solver.cs | 22 +++--- dev4/psychlops/extention/standard/widget.cs | 2 +- 7 files changed, 201 insertions(+), 79 deletions(-) create mode 100644 dev4/psychlops/extention/compatibility/compatibility.cs diff --git a/dev4/psychlops/core/graphic/canvas.cs b/dev4/psychlops/core/graphic/canvas.cs index c129d82..be3cbae 100644 --- a/dev4/psychlops/core/graphic/canvas.cs +++ b/dev4/psychlops/core/graphic/canvas.cs @@ -31,21 +31,21 @@ namespace Psychlops { // protected System.Collections.Generic.Queue stack; internal Internal.PrimitiveFigure[] stack; - internal int stackN; + internal int stackN = 0; internal Line[] lineStack; - internal int lineStackN; + internal int lineStackN = 0; internal Rectangle[] rectStack; - internal int rectStackN; + internal int rectStackN = 0; internal Ellipse[] ellipseStack; - internal int ellipseStackN; + internal int ellipseStackN = 0; internal Polygon[] polygonStack; - internal int polygonStackN; + internal int polygonStackN = 0; internal Letters[] lettersStack; - internal int lettersStackN; + internal int lettersStackN = 0; internal Image[] imageStack; - internal int imageStackN; + internal int imageStackN = 0; internal Group[] groupStack; - internal int groupStackN; + internal int groupStackN = 0; public StackableDrawable() @@ -648,7 +648,7 @@ namespace Psychlops api_canvas = default_api_canvas; initialize(500, 500); } - public Canvas(Mode mod) + public Canvas(Mode mod) : base() { panel = default_panel; api_canvas = default_api_canvas; diff --git a/dev4/psychlops/core/math/interval.cs b/dev4/psychlops/core/math/interval.cs index 0b9a22d..843338f 100644 --- a/dev4/psychlops/core/math/interval.cs +++ b/dev4/psychlops/core/math/interval.cs @@ -9,7 +9,7 @@ namespace Psychlops public enum OPERATOR { CLOSE, OPEN }; public const OPERATOR CLOSE = OPERATOR.CLOSE, OPEN = OPERATOR.OPEN; public struct VAL { - public double val; + public double value; public OPERATOR op; /*public VAL() { @@ -18,12 +18,12 @@ namespace Psychlops }*/ public VAL(double v, OPERATOR o) { - val = v; + value = v; op = o; } public bool bounded() { - return !Double.IsNaN(val) && (!Double.IsInfinity(val) || op == OPERATOR.OPEN); + return !Double.IsNaN(value) && (!Double.IsInfinity(value) || op == OPERATOR.OPEN); } } public VAL begin, end; @@ -36,43 +36,65 @@ namespace Psychlops }*/ public Interval(double floor_val, double ceil_val) { - begin.val = floor_val; + begin.value = floor_val; begin.op = OPERATOR.CLOSE; - end.val = ceil_val; + end.value = ceil_val; end.op = OPERATOR.CLOSE; } public Interval(double floor_val, OPERATOR floor_op, double ceil_val, OPERATOR ceil_op) { - begin.val = floor_val; + begin.value = floor_val; begin.op = floor_op; - end.val = ceil_val; + end.value = ceil_val; end.op = ceil_op; } - /* - public int int_floor(); - public int int_floor(int minval); - public int int_ceil(); - public int int_ceil(int maxval); - */ + + public int int_floor() + { + double v = Math.ceil(begin.value); + if (begin.op == OPEN && v == begin.value) { return (int)v + 1; } + else return (int)v; + } + public int int_floor(int minval) + { + if(begin.valuemaxval) return maxval; + double v = Math.floor(end.value); + if (end.op == OPEN && v == end.value) { return (int)v - 1; } + else return (int)v; + } + bool includes(double val) { bool result = false; switch(begin.op) { case OPERATOR.CLOSE: - result = begin.val<=val ? true : false; - break; + result = begin.value <= val ? true : false; + break; case OPERATOR.OPEN: - result = begin.val=val ? true : false ); - break; + result = result && (end.value >= val ? true : false); + break; case OPERATOR.OPEN: - result = result && ( end.val>val ? true : false ); - break; + result = result && (end.value > val ? true : false); + break; } return result; } @@ -88,13 +110,13 @@ namespace Psychlops // return new IntervalIEnumerable(this, steps); Interval it = this; long front_step = (it.begin.op == Interval.OPERATOR.CLOSE ? -1 : 0); - long back_step = (long)System.Math.Floor((it.end.val - it.begin.val) / steps); - if (it.end.op == Interval.OPERATOR.OPEN && 0 == System.Math.IEEERemainder(it.end.val - it.begin.val, steps)) + long back_step = (long)System.Math.Floor((it.end.value - it.begin.value) / steps); + if (it.end.op == Interval.OPERATOR.OPEN && 0 == System.Math.IEEERemainder(it.end.value - it.begin.value, steps)) { back_step -= 1; } while (front_step <= back_step) - yield return steps * front_step + it.begin.val; + yield return steps * front_step + it.begin.value; } @@ -147,35 +169,35 @@ namespace Psychlops public static IntervalAcc operator <(double val, IntervalAcc rng) { - return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.OPEN, rng.instance.end.val, rng.instance.end.op) }; + return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.OPEN, rng.instance.end.value, rng.instance.end.op) }; } public static IntervalAcc operator <=(double val, IntervalAcc rng) { - return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.CLOSE, rng.instance.end.val, rng.instance.end.op) }; + return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.CLOSE, rng.instance.end.value, rng.instance.end.op) }; } public static IntervalAcc operator >(double val, IntervalAcc rng) { - return new IntervalAcc { instance = new Interval(rng.instance.begin.val, rng.instance.begin.op, val, Interval.OPERATOR.OPEN) }; + return new IntervalAcc { instance = new Interval(rng.instance.begin.value, rng.instance.begin.op, val, Interval.OPERATOR.OPEN) }; } public static IntervalAcc operator >=(double val, IntervalAcc rng) { - return new IntervalAcc { instance = new Interval(rng.instance.begin.val, rng.instance.begin.op, val, Interval.OPERATOR.CLOSE) }; + return new IntervalAcc { instance = new Interval(rng.instance.begin.value, rng.instance.begin.op, val, Interval.OPERATOR.CLOSE) }; } public static IntervalAcc operator <(IntervalAcc rng, double val) { - return new IntervalAcc { instance = new Interval(rng.instance.begin.val, rng.instance.begin.op, val, Interval.OPERATOR.OPEN) }; + return new IntervalAcc { instance = new Interval(rng.instance.begin.value, rng.instance.begin.op, val, Interval.OPERATOR.OPEN) }; } public static IntervalAcc operator <=(IntervalAcc rng, double val) { - return new IntervalAcc { instance = new Interval(rng.instance.begin.val, rng.instance.begin.op, val, Interval.OPERATOR.CLOSE) }; + return new IntervalAcc { instance = new Interval(rng.instance.begin.value, rng.instance.begin.op, val, Interval.OPERATOR.CLOSE) }; } public static IntervalAcc operator >(IntervalAcc rng, double val) { - return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.OPEN, rng.instance.end.val, rng.instance.end.op) }; + return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.OPEN, rng.instance.end.value, rng.instance.end.op) }; } public static IntervalAcc operator >=(IntervalAcc rng, double val) { - return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.CLOSE, rng.instance.end.val, rng.instance.end.op) }; + return new IntervalAcc { instance = new Interval(val, Interval.OPERATOR.CLOSE, rng.instance.end.value, rng.instance.end.op) }; } public static implicit operator Interval(IntervalAcc rhs) diff --git a/dev4/psychlops/core/math/matrix.cs b/dev4/psychlops/core/math/matrix.cs index 9bd895f..de4a836 100644 --- a/dev4/psychlops/core/math/matrix.cs +++ b/dev4/psychlops/core/math/matrix.cs @@ -13,11 +13,25 @@ namespace Psychlops } /* + public abstract Internal.MatrixExpression this[Interval row, Interval col] + { + get; + set; + } + + + /* public static Matrix operator +(Matrix m, double d) { return new Internal.MatrixExpression(); } */ + + public abstract int rows { get; } + public abstract int cols { get; } + public int getRows() { return rows; } + public int getCols() { return cols; } + } @@ -26,41 +40,56 @@ namespace Psychlops public class MatrixImplementation : Matrix { - internal double[] elements; - readonly int nrow, ncol; + internal double[,] elements; public MatrixImplementation(int dnrow, int dncol) { - nrow = dnrow; - ncol = dncol; - elements = new double[nrow * ncol]; + elements = new double[dnrow, dncol]; } public override double this[int row, int col] { get { - return elements[(row - 1) * ncol + (col - 1)]; + return elements[row - 1, col - 1]; } set { - elements[(row - 1) * ncol + (col - 1)] = value; + elements[row - 1, col - 1] = value; } } + /* + public override MatrixExpression this[Interval row, Interval col] + { + get + { + return new MatrixExpression(this, row.int_floor(), col.int_floor(), row.int_ceil(), col.int_ceil()); + } + set + { + for(int r = 0, r<) + elements[row - 1, col - 1] = value; + } + } + * */ + + public override int rows { get { return elements.GetLength(0); } } + public override int cols { get { return elements.GetLength(1); } } + } public class MatrixExpression : Matrix { MatrixImplementation imp; - readonly int nrow, ncol; - readonly int drow, dcol; - - public MatrixExpression(MatrixImplementation target, int ddrow, int ddcol, int dnrow, int dncol) + readonly int brow, bcol; + readonly int erow, ecol; + + internal MatrixExpression(MatrixImplementation target, int dbrow, int dbcol, int derow, int decol) { - nrow = dnrow; - ncol = dncol; - drow = ddrow; - dcol = ddcol; + brow = dbrow; + bcol = dbcol; + erow = derow; + ecol = decol; imp = target; } @@ -68,13 +97,16 @@ namespace Psychlops { get { - return imp.elements[(row - drow) * ncol + (col - dcol)]; + return imp.elements[(row - 1 - brow), (col - 1 - bcol)]; } set { - imp.elements[(row - drow) * ncol + (col - dcol)] = value; + imp.elements[(row - 1 - brow), (col - 1 - bcol)] = value; } } + + public override int rows { get { return erow - brow + 1; } } + public override int cols { get { return ecol - bcol + 1; } } } } diff --git a/dev4/psychlops/core/math/util.cs b/dev4/psychlops/core/math/util.cs index 338423a..20886e0 100644 --- a/dev4/psychlops/core/math/util.cs +++ b/dev4/psychlops/core/math/util.cs @@ -5,20 +5,20 @@ namespace Psychlops public static class Math { - public static readonly double PI = 3.14159265, E = 2.718281828459045, LOG2E = 1.44269504088896340736; + public const double PI = 3.14159265, E = 2.718281828459045, LOG2E = 1.44269504088896340736; public static Random random_generator; static Math() { random_generator = new Random(); } - public static double max(double val1, double val2) + public static T max(T val1, T val2) where T : IComparable { - return val1 > val2 ? val1 : val2; + return val1.CompareTo(val2) > 0 ? val1 : val2; } - public static double min(double val1, double val2) + public static T min(T val1, T val2) where T : IComparable { - return val1 < val2 ? val1 : val2; + return val1.CompareTo(val2) < 0 ? val1 : val2; } public static void shuffle(X[] array, int n) { @@ -41,6 +41,14 @@ namespace Psychlops { return System.Math.Abs(x); } + public static double floor(double x) + { + return System.Math.Floor(x); + } + public static double ceil(double x) + { + return System.Math.Ceiling(x); + } public static double sin(double x) { return System.Math.Sin(x); @@ -53,6 +61,26 @@ namespace Psychlops { return System.Math.Tan(x); } + public static double asin(double x) + { + return System.Math.Asin(x); + } + public static double acos(double x) + { + return System.Math.Acos(x); + } + public static double atan(double x) + { + return System.Math.Atan(x); + } + public static double atan(double y, double x) + { + return System.Math.Atan2(y, x); + } + public static double atan2(double y, double x) + { + return System.Math.Atan2(y, x); + } public static double sqrt(double x) { return System.Math.Sqrt(x); @@ -69,11 +97,10 @@ namespace Psychlops { return log(val) * LOG2E; } - /*public static int round(double val) + public static double round(double val) { - double integer_part, particle = modf(val, &integer_part); - return ((particle < 0.5 | (particle == 0.5 && (int)integer_part % 2 == 0)) ? (int)integer_part : (int)integer_part + 1); - }*/ + return System.Math.Round(val); + } public static double radius(double x, double y) { diff --git a/dev4/psychlops/extention/compatibility/compatibility.cs b/dev4/psychlops/extention/compatibility/compatibility.cs new file mode 100644 index 0000000..bedc26f --- /dev/null +++ b/dev4/psychlops/extention/compatibility/compatibility.cs @@ -0,0 +1,41 @@ + + + +namespace Psychlops +{ + + public static class Display + { + public static void pix(int x, int y, Color col) { Main.canvas.pix(x, y, col); } + public static void line(Line drawee) { Main.canvas.line(drawee); } + public static void rect(Rectangle drawee) { Main.canvas.rect(drawee); } + public static void ellipse(Ellipse drawee) { Main.canvas.ellipse(drawee); } + public static void polygon(Polygon drawee) { Main.canvas.polygon(drawee); } + public static void letters(Letters drawee) { Main.canvas.letters(drawee); } + public static void image(Image drawee) { Main.canvas.image(drawee); } + public static void group(Group drawee) { Main.canvas.group(drawee); } + + + public static void msg(string str, double x, double y) { Main.canvas.msg(str, x, y, Color.white); } + public static void msg(string dstr, double x, double y, Color col) { Main.canvas.msg(dstr, x, y, col); } + public static void var(Type val, double x, double y) { Main.canvas.var(val, x, y, Color.white); } + public static void var(Type val, double x, double y, Color col) { Main.canvas.var(val, x, y, col); } + + + + public static void clear(double lum) { Main.canvas.clear(lum); } + public static void clear(Color col) { Main.canvas.clear(col); } + public static void flip(int n = 1) { Main.canvas.flip(n); } + + public static double width { get { return Main.canvas.width; } } + public static double height { get { return Main.canvas.height; } } + public static Point center { get { return Main.canvas.center; } } + public static double getWidth() { return width; } + public static double getHeight() { return height; } + public static Point getCenter() { return center; } + public static double getHCenter() { return Main.canvas.getHCenter(); } + public static double getVCenter() { return Main.canvas.getVCenter(); } + public static double getRefreshRate() { return Main.canvas.getRefreshRate(); } + } + +} diff --git a/dev4/psychlops/extention/math/solver.cs b/dev4/psychlops/extention/math/solver.cs index 0a149ec..2bca111 100644 --- a/dev4/psychlops/extention/math/solver.cs +++ b/dev4/psychlops/extention/math/solver.cs @@ -146,7 +146,7 @@ namespace Psychlops double like = 1.0; champ_like=0.0; int L = data.length; - for (double a = itvl[0].begin.val; a < itvl[0].end.val; a += step[0]) + for (double a = itvl[0].begin.value; a < itvl[0].end.value; a += step[0]) { like = 1.0; for(int i=0; i itvl[j].end.val ? itvl[j].end.val : champ[j] + r / 8.0; + r = itvl[j].end.value - itvl[j].begin.value; + low = champ[j] - r / 8.0 < itvl[j].begin.value ? itvl[j].begin.value : champ[j] - r / 8.0; + high = champ[j] + r / 8.0 > itvl[j].end.value ? itvl[j].end.value : champ[j] + r / 8.0; itvl[j] = new Interval(low, high); } diff --git a/dev4/psychlops/extention/standard/widget.cs b/dev4/psychlops/extention/standard/widget.cs index 20cbfa3..42e7668 100644 --- a/dev4/psychlops/extention/standard/widget.cs +++ b/dev4/psychlops/extention/standard/widget.cs @@ -299,7 +299,7 @@ namespace Psychlops var val = new System.Windows.Controls.TextBox { Width = 100 }; System.Windows.Controls.Canvas.SetLeft(val, 100); var slide = new System.Windows.Controls.Slider { - Minimum = ss.range.begin.val, Maximum = ss.range.end.val, + Minimum = ss.range.begin.value, Maximum = ss.range.end.value, Name = ss.label, Value = ss.retval, Tag = ss, Width = 200, -- 2.11.0