├── .gitignore └── FunctionVisualizer ├── Cloo.dll ├── Cloo.dll.config ├── FunctionVisualizer.sln ├── FvCalculation ├── ExpressionExtensions.cs ├── ExpressionParser.cs ├── FunctionExpressions │ ├── CosExpression.cs │ ├── CotExpression.cs │ ├── CscExpression.cs │ ├── ExpExpression.cs │ ├── LnExpression.cs │ ├── SecExpression.cs │ ├── SinExpression.cs │ └── TanExpression.cs ├── FvCalculation.csproj ├── OpenClExtensions.cs ├── OperatorExpressions │ ├── AddExpression.cs │ ├── DivExpression.cs │ ├── MulExpression.cs │ ├── NegExpression.cs │ ├── PowerExpression.cs │ └── SubExpression.cs ├── PrimitiveExpressions │ ├── NumberExpression.cs │ └── VariableExpression.cs ├── Properties │ └── AssemblyInfo.cs └── RawExpression.cs └── FvGUI ├── FvGUI.csproj ├── GpuHelper.cs ├── GpuHelperFactory.cs ├── IGpuHelper.cs ├── MainForm.Designer.cs ├── MainForm.cs ├── MainForm.resx ├── MainFormRenderer.cs ├── OpenClSetting.Designer.cs ├── OpenClSetting.cs ├── OpenClSetting.resx ├── Program.cs ├── Properties ├── AssemblyInfo.cs ├── Resources.Designer.cs ├── Resources.resx ├── Settings.Designer.cs └── Settings.settings └── function_vis.cl /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Visual Studio 3 | ################# 4 | 5 | ## Ignore Visual Studio temporary files, build results, and 6 | ## files generated by popular Visual Studio add-ons. 7 | 8 | # User-specific files 9 | *.suo 10 | *.user 11 | *.sln.docstates 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Rr]elease/ 16 | *_i.c 17 | *_p.c 18 | *.ilk 19 | *.meta 20 | *.obj 21 | *.pch 22 | *.pdb 23 | *.pgc 24 | *.pgd 25 | *.rsp 26 | *.sbr 27 | *.tlb 28 | *.tli 29 | *.tlh 30 | *.tmp 31 | *.vspscc 32 | .builds 33 | *.dotCover 34 | 35 | ## TODO: If you have NuGet Package Restore enabled, uncomment this 36 | #packages/ 37 | 38 | # Visual C++ cache files 39 | ipch/ 40 | *.aps 41 | *.ncb 42 | *.opensdf 43 | *.sdf 44 | 45 | # Visual Studio profiler 46 | *.psess 47 | *.vsp 48 | 49 | # ReSharper is a .NET coding add-in 50 | _ReSharper* 51 | 52 | # Installshield output folder 53 | [Ee]xpress 54 | 55 | # DocProject is a documentation generator add-in 56 | DocProject/buildhelp/ 57 | DocProject/Help/*.HxT 58 | DocProject/Help/*.HxC 59 | DocProject/Help/*.hhc 60 | DocProject/Help/*.hhk 61 | DocProject/Help/*.hhp 62 | DocProject/Help/Html2 63 | DocProject/Help/html 64 | 65 | # Click-Once directory 66 | publish 67 | 68 | # Others 69 | [Bb]in 70 | [Oo]bj 71 | sql 72 | TestResults 73 | *.Cache 74 | ClientBin 75 | stylecop.* 76 | ~$* 77 | *.dbmdl 78 | Generated_Code #added for RIA/Silverlight projects 79 | 80 | # Backup & report files from converting an old project file to a newer 81 | # Visual Studio version. Backup files are not needed, because we have git ;-) 82 | _UpgradeReport_Files/ 83 | Backup*/ 84 | UpgradeLog*.XML 85 | 86 | 87 | 88 | ############ 89 | ## Windows 90 | ############ 91 | 92 | # Windows image file caches 93 | Thumbs.db 94 | 95 | # Folder config file 96 | Desktop.ini -------------------------------------------------------------------------------- /FunctionVisualizer/Cloo.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ninputer/opencl-plot/c01f87881aca460353144b2a88a39f4edf889867/FunctionVisualizer/Cloo.dll -------------------------------------------------------------------------------- /FunctionVisualizer/Cloo.dll.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /FunctionVisualizer/FunctionVisualizer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FvCalculation", "FvCalculation\FvCalculation.csproj", "{F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FvGUI", "FvGUI\FvGUI.csproj", "{C5F3E283-2745-42EC-BD8B-1BAC88202164}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Debug|Mixed Platforms = Debug|Mixed Platforms 12 | Debug|x86 = Debug|x86 13 | Release|Any CPU = Release|Any CPU 14 | Release|Mixed Platforms = Release|Mixed Platforms 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU 21 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU 22 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Debug|x86.ActiveCfg = Debug|Any CPU 23 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU 26 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Release|Mixed Platforms.Build.0 = Release|Any CPU 27 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4}.Release|x86.ActiveCfg = Release|Any CPU 28 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Debug|Any CPU.ActiveCfg = Debug|x86 29 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 30 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Debug|Mixed Platforms.Build.0 = Debug|x86 31 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Debug|x86.ActiveCfg = Debug|x86 32 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Debug|x86.Build.0 = Debug|x86 33 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Release|Any CPU.ActiveCfg = Release|x86 34 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Release|Mixed Platforms.ActiveCfg = Release|x86 35 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Release|Mixed Platforms.Build.0 = Release|x86 36 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Release|x86.ActiveCfg = Release|x86 37 | {C5F3E283-2745-42EC-BD8B-1BAC88202164}.Release|x86.Build.0 = Release|x86 38 | EndGlobalSection 39 | GlobalSection(SolutionProperties) = preSolution 40 | HideSolutionNode = FALSE 41 | EndGlobalSection 42 | EndGlobal 43 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/ExpressionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace FvCalculation 7 | { 8 | public static class ExpressionExtensions 9 | { 10 | public static double Execute(this RawExpression e, string name, double value) 11 | { 12 | return e.Execute(new Dictionary { { name, value } }); 13 | } 14 | 15 | public static RawExpression Apply(this RawExpression e, string name, double value) 16 | { 17 | return e.Apply(new Dictionary { { name, value } }); 18 | } 19 | 20 | public static double Solve(this RawExpression e, string name, double start, int maxCount = 1000) 21 | { 22 | RawExpression f = e.Simplify(); 23 | RawExpression df = f.Different(name).Simplify(); 24 | return f.Solve(df, name, start, maxCount); 25 | } 26 | 27 | public static double Solve(this RawExpression f, RawExpression df, string name, double start, int maxCount = 1000) 28 | { 29 | return Solve((x) => f.Execute(name, x), (x) => df.Execute(name, x), start, maxCount); 30 | } 31 | 32 | public static double Solve(this Func f, Func df, double start, int maxCount = 1000) 33 | { 34 | for (int i = 0; i < maxCount; i++) 35 | { 36 | double result = f(start); 37 | if (-RawExpression.ZeroNumber <= result && result <= RawExpression.ZeroNumber) 38 | { 39 | return start; 40 | } 41 | 42 | double d = df(start); 43 | if (-RawExpression.ZeroNumber <= d && d <= RawExpression.ZeroNumber) 44 | { 45 | return double.NaN; 46 | } 47 | else 48 | { 49 | start -= result / d; 50 | } 51 | } 52 | return double.NaN; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/ExpressionParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.OperatorExpressions; 6 | using FvCalculation.PrimitiveExpressions; 7 | 8 | namespace FvCalculation 9 | { 10 | unsafe static class ExpressionParser 11 | { 12 | private static void SkipSpaces(char** input) 13 | { 14 | while (char.IsWhiteSpace(**input)) 15 | { 16 | (*input)++; 17 | } 18 | } 19 | 20 | private static bool Char(char** input, char c) 21 | { 22 | SkipSpaces(input); 23 | if (**input == c) 24 | { 25 | (*input)++; 26 | return true; 27 | } 28 | else 29 | { 30 | return false; 31 | } 32 | } 33 | 34 | private static double Number(char** input) 35 | { 36 | SkipSpaces(input); 37 | bool dotted = false; 38 | string s = ""; 39 | while (true) 40 | { 41 | if ('0' <= **input && **input <= '9') 42 | { 43 | s += **input; 44 | (*input)++; 45 | } 46 | else if ('.' == **input && !dotted) 47 | { 48 | dotted = true; 49 | s += **input; 50 | (*input)++; 51 | } 52 | else 53 | { 54 | break; 55 | } 56 | } 57 | if (s == "") 58 | { 59 | return double.NaN; 60 | } 61 | else 62 | { 63 | return double.Parse(s); 64 | } 65 | } 66 | 67 | private static string Name(char** input) 68 | { 69 | SkipSpaces(input); 70 | string s = ""; 71 | while (true) 72 | { 73 | if (('a' <= **input && **input <= 'z') || ('A' <= **input && **input <= 'Z') || ('_' == **input) || (s != "" && '0' <= **input && **input <= '9')) 74 | { 75 | s += **input; 76 | (*input)++; 77 | } 78 | else 79 | { 80 | break; 81 | } 82 | } 83 | return s == "" ? null : s; 84 | } 85 | 86 | private static RawExpression Exp0(char** input) 87 | { 88 | if (Char(input, '(')) 89 | { 90 | RawExpression e = Exp3(input); 91 | if (!Char(input, ')')) 92 | { 93 | throw new ArgumentException("Error encountered, at " + new string(*input)); 94 | } 95 | return e; 96 | } 97 | else if (Char(input, '-')) 98 | { 99 | return new NegExpression 100 | { 101 | Op = Exp0(input), 102 | }; 103 | } 104 | else 105 | { 106 | double number = Number(input); 107 | if (!double.IsNaN(number)) 108 | { 109 | return new NumberExpression 110 | { 111 | Number = number, 112 | }; 113 | } 114 | 115 | string name = Name(input); 116 | if (name == null) 117 | { 118 | throw new ArgumentException("Error encountered, at " + new string(*input)); 119 | } 120 | 121 | if (!Char(input, '(')) 122 | { 123 | return new VariableExpression 124 | { 125 | Name = name, 126 | }; 127 | } 128 | 129 | FunctionExpression f = FunctionExpression.FromName(name); 130 | f.Op = Exp3(input); 131 | if (!Char(input, ')')) 132 | { 133 | throw new ArgumentException("Error encountered, at " + new string(*input)); 134 | } 135 | return f; 136 | } 137 | } 138 | 139 | private static RawExpression Exp1(char** input) 140 | { 141 | RawExpression e = Exp0(input); 142 | while (true) 143 | { 144 | if (Char(input, '^')) 145 | { 146 | e = new PowerExpression 147 | { 148 | Left = e, 149 | Right = Exp0(input), 150 | }; 151 | } 152 | else 153 | { 154 | break; 155 | } 156 | } 157 | return e; 158 | } 159 | 160 | private static RawExpression Exp2(char** input) 161 | { 162 | RawExpression e = Exp1(input); 163 | while (true) 164 | { 165 | if (Char(input, '*')) 166 | { 167 | e = new MulExpression 168 | { 169 | Left = e, 170 | Right = Exp1(input), 171 | }; 172 | } 173 | else if (Char(input, '/')) 174 | { 175 | e = new DivExpression 176 | { 177 | Left = e, 178 | Right = Exp1(input), 179 | }; 180 | } 181 | else 182 | { 183 | break; 184 | } 185 | } 186 | return e; 187 | } 188 | 189 | private static RawExpression Exp3(char** input) 190 | { 191 | RawExpression e = Exp2(input); 192 | while (true) 193 | { 194 | if (Char(input, '+')) 195 | { 196 | e = new AddExpression 197 | { 198 | Left = e, 199 | Right = Exp2(input), 200 | }; 201 | } 202 | else if (Char(input, '-')) 203 | { 204 | e = new SubExpression 205 | { 206 | Left = e, 207 | Right = Exp2(input), 208 | }; 209 | } 210 | else 211 | { 212 | break; 213 | } 214 | } 215 | return e; 216 | } 217 | 218 | private static RawExpression UnsafeParse(char* input) 219 | { 220 | RawExpression result = Exp3(&input); 221 | if ((int)*input == 0) 222 | { 223 | return result; 224 | } 225 | else 226 | { 227 | throw new ArgumentException("Expression contains unparsed tail, at " + new string(input)); 228 | } 229 | } 230 | 231 | public static RawExpression Parse(string s) 232 | { 233 | fixed (char* input = s.Trim()) 234 | { 235 | return UnsafeParse(input); 236 | } 237 | } 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FunctionExpressions/CosExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Linq.Expressions; 6 | using FvCalculation.OperatorExpressions; 7 | 8 | namespace FvCalculation.FunctionExpressions 9 | { 10 | [FunctionName("cos")] 11 | class CosExpression : FunctionExpression 12 | { 13 | public override double Execute(Dictionary variables) 14 | { 15 | return Math.Cos(this.Op.Execute(variables)); 16 | } 17 | 18 | public override RawExpression Different(string variable) 19 | { 20 | return new MulExpression 21 | { 22 | Left = new NegExpression 23 | { 24 | Op = new SinExpression 25 | { 26 | Op = this.Op, 27 | }, 28 | }, 29 | Right = this.Op.Different(variable), 30 | }; 31 | } 32 | 33 | public override Expression CompileInternal(Dictionary parameters) 34 | { 35 | return Expression.Call(typeof(Math).GetMethod("Cos", new Type[] { typeof(double) }), this.Op.CompileInternal(parameters)); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FunctionExpressions/CotExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Linq.Expressions; 6 | using FvCalculation.OperatorExpressions; 7 | using FvCalculation.PrimitiveExpressions; 8 | 9 | namespace FvCalculation.FunctionExpressions 10 | { 11 | [FunctionName("cot")] 12 | class CotExpression : FunctionExpression 13 | { 14 | public override double Execute(Dictionary variables) 15 | { 16 | return 1 / Math.Tan(this.Op.Execute(variables)); 17 | } 18 | 19 | public override RawExpression Different(string variable) 20 | { 21 | return new MulExpression 22 | { 23 | Left = new NegExpression 24 | { 25 | Op = new PowerExpression 26 | { 27 | Left = new CscExpression 28 | { 29 | Op = this.Op, 30 | }, 31 | Right = new NumberExpression 32 | { 33 | Number = 2, 34 | }, 35 | }, 36 | }, 37 | Right = this.Op.Different(variable), 38 | }; 39 | } 40 | 41 | public override Expression CompileInternal(Dictionary parameters) 42 | { 43 | return Expression.Divide( 44 | Expression.Constant(1.0), 45 | Expression.Call(typeof(Math).GetMethod("Tan", new Type[] { typeof(double) }), this.Op.CompileInternal(parameters)) 46 | ); 47 | } 48 | 49 | public override string ToCode() 50 | { 51 | return "(_F(1.0)/tan(" + this.Op.ToCode() + "))"; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FunctionExpressions/CscExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Linq.Expressions; 6 | using FvCalculation.OperatorExpressions; 7 | 8 | namespace FvCalculation.FunctionExpressions 9 | { 10 | [FunctionName("csc")] 11 | class CscExpression : FunctionExpression 12 | { 13 | public override double Execute(Dictionary variables) 14 | { 15 | return 1 / Math.Sin(this.Op.Execute(variables)); 16 | } 17 | 18 | public override RawExpression Different(string variable) 19 | { 20 | return new MulExpression 21 | { 22 | Left = new NegExpression 23 | { 24 | Op = new MulExpression 25 | { 26 | Left = new CscExpression 27 | { 28 | Op = this.Op, 29 | }, 30 | Right = new CotExpression 31 | { 32 | Op = this.Op, 33 | }, 34 | }, 35 | }, 36 | Right = this.Op.Different(variable), 37 | }; 38 | } 39 | 40 | public override Expression CompileInternal(Dictionary parameters) 41 | { 42 | return Expression.Divide( 43 | Expression.Constant(1.0), 44 | Expression.Call(typeof(Math).GetMethod("Sin", new Type[] { typeof(double) }), this.Op.CompileInternal(parameters)) 45 | ); 46 | } 47 | 48 | public override string ToCode() 49 | { 50 | return "(_F(1.0)/sin(" + this.Op.ToCode() + "))"; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FunctionExpressions/ExpExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.OperatorExpressions; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation.FunctionExpressions 9 | { 10 | [FunctionName("exp")] 11 | class ExpExpression : FunctionExpression 12 | { 13 | public override double Execute(Dictionary variables) 14 | { 15 | return Math.Exp(this.Op.Execute(variables)); 16 | } 17 | 18 | public override RawExpression Different(string variable) 19 | { 20 | return new MulExpression 21 | { 22 | Left = this, 23 | Right = this.Op.Different(variable), 24 | }; 25 | } 26 | 27 | public override Expression CompileInternal(Dictionary parameters) 28 | { 29 | return Expression.Call(typeof(Math).GetMethod("Exp", new Type[] { typeof(double) }), this.Op.CompileInternal(parameters)); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FunctionExpressions/LnExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.OperatorExpressions; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation.FunctionExpressions 9 | { 10 | [FunctionName("ln")] 11 | class LnExpression : FunctionExpression 12 | { 13 | public override double Execute(Dictionary variables) 14 | { 15 | return Math.Log(this.Op.Execute(variables)); 16 | } 17 | 18 | public override RawExpression Different(string variable) 19 | { 20 | return new DivExpression 21 | { 22 | Left = this.Op.Different(variable), 23 | Right = this.Op, 24 | }; 25 | } 26 | 27 | public override Expression CompileInternal(Dictionary parameters) 28 | { 29 | return Expression.Call(typeof(Math).GetMethod("Log", new Type[] { typeof(double) }), this.Op.CompileInternal(parameters)); 30 | } 31 | 32 | public override string ToCode() 33 | { 34 | return "log(" + Op.ToCode() + ")"; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FunctionExpressions/SecExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Linq.Expressions; 6 | using FvCalculation.OperatorExpressions; 7 | 8 | namespace FvCalculation.FunctionExpressions 9 | { 10 | [FunctionName("sec")] 11 | class SecExpression : FunctionExpression 12 | { 13 | public override double Execute(Dictionary variables) 14 | { 15 | return 1/Math.Cos(this.Op.Execute(variables)); 16 | } 17 | 18 | public override RawExpression Different(string variable) 19 | { 20 | return new MulExpression 21 | { 22 | Left = new MulExpression 23 | { 24 | Left = new SecExpression 25 | { 26 | Op = this.Op, 27 | }, 28 | Right = new TanExpression 29 | { 30 | Op = this.Op, 31 | }, 32 | }, 33 | Right = this.Op.Different(variable), 34 | }; 35 | } 36 | 37 | public override Expression CompileInternal(Dictionary parameters) 38 | { 39 | return Expression.Divide( 40 | Expression.Constant(1.0), 41 | Expression.Call(typeof(Math).GetMethod("Cos", new Type[] { typeof(double) }), this.Op.CompileInternal(parameters)) 42 | ); 43 | } 44 | 45 | public override string ToCode() 46 | { 47 | return "(_F(1.0)/cos(" + this.Op.ToCode() + "))"; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FunctionExpressions/SinExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Linq.Expressions; 6 | using FvCalculation.OperatorExpressions; 7 | 8 | namespace FvCalculation.FunctionExpressions 9 | { 10 | [FunctionName("sin")] 11 | class SinExpression : FunctionExpression 12 | { 13 | public override double Execute(Dictionary variables) 14 | { 15 | return Math.Sin(this.Op.Execute(variables)); 16 | } 17 | 18 | public override RawExpression Different(string variable) 19 | { 20 | return new MulExpression 21 | { 22 | Left = new CosExpression 23 | { 24 | Op = this.Op, 25 | }, 26 | Right = this.Op.Different(variable), 27 | }; 28 | } 29 | 30 | public override Expression CompileInternal(Dictionary parameters) 31 | { 32 | return Expression.Call(typeof(Math).GetMethod("Sin", new Type[] { typeof(double) }), this.Op.CompileInternal(parameters)); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FunctionExpressions/TanExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Linq.Expressions; 6 | using FvCalculation.OperatorExpressions; 7 | using FvCalculation.PrimitiveExpressions; 8 | 9 | namespace FvCalculation.FunctionExpressions 10 | { 11 | [FunctionName("tan")] 12 | class TanExpression : FunctionExpression 13 | { 14 | public override double Execute(Dictionary variables) 15 | { 16 | return Math.Tan(this.Op.Execute(variables)); 17 | } 18 | 19 | public override RawExpression Different(string variable) 20 | { 21 | return new MulExpression 22 | { 23 | Left = new PowerExpression 24 | { 25 | Left = new SecExpression 26 | { 27 | Op = this.Op, 28 | }, 29 | Right = new NumberExpression 30 | { 31 | Number = 2, 32 | }, 33 | }, 34 | Right = this.Op.Different(variable), 35 | }; 36 | } 37 | 38 | public override Expression CompileInternal(Dictionary parameters) 39 | { 40 | return Expression.Call(typeof(Math).GetMethod("Tan", new Type[] { typeof(double) }), this.Op.CompileInternal(parameters)); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/FvCalculation.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4} 9 | Library 10 | Properties 11 | FvCalculation 12 | FvCalculation 13 | v4.0 14 | 512 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | true 26 | full 27 | false 28 | bin\Debug\ 29 | DEBUG;TRACE 30 | prompt 31 | 4 32 | true 33 | 34 | 35 | pdbonly 36 | true 37 | bin\Release\ 38 | TRACE 39 | prompt 40 | 4 41 | true 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 83 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/OpenClExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace FvCalculation 7 | { 8 | class OpenClExtensions 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/OperatorExpressions/AddExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.PrimitiveExpressions; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation.OperatorExpressions 9 | { 10 | class AddExpression : RawExpression 11 | { 12 | public RawExpression Left { get; set; } 13 | public RawExpression Right { get; set; } 14 | 15 | public override double Execute(Dictionary variables) 16 | { 17 | return this.Left.Execute(variables) + this.Right.Execute(variables); 18 | } 19 | 20 | public override RawExpression Apply(Dictionary variables) 21 | { 22 | return new AddExpression 23 | { 24 | Left = this.Left.Apply(variables), 25 | Right = this.Right.Apply(variables), 26 | }; 27 | } 28 | 29 | public override RawExpression Different(string variable) 30 | { 31 | return new AddExpression 32 | { 33 | Left = this.Left.Different(variable), 34 | Right = this.Right.Different(variable), 35 | }; 36 | } 37 | 38 | public override bool ContainsVariable(string variable) 39 | { 40 | return this.Left.ContainsVariable(variable) || this.Right.ContainsVariable(variable); 41 | } 42 | 43 | public override RawExpression SimplifyInternal() 44 | { 45 | RawExpression sleft = this.Left.Simplify(); 46 | RawExpression sright = this.Right.Simplify(); 47 | NumberExpression nleft = sleft as NumberExpression; 48 | NumberExpression nright = sright as NumberExpression; 49 | if (nleft != null && nleft.Number == 0) 50 | { 51 | return sright; 52 | } 53 | else if (nright != null && nright.Number == 0) 54 | { 55 | return sleft; 56 | } 57 | else 58 | { 59 | return new AddExpression 60 | { 61 | Left = sleft, 62 | Right = sright, 63 | }; 64 | } 65 | } 66 | 67 | public override Expression CompileInternal(Dictionary parameters) 68 | { 69 | return Expression.Add(this.Left.CompileInternal(parameters), this.Right.CompileInternal(parameters)); 70 | } 71 | 72 | public override string ToCode() 73 | { 74 | return "(" + this.Left.ToCode() + " + " + this.Right.ToCode() + ")"; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/OperatorExpressions/DivExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.PrimitiveExpressions; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation.OperatorExpressions 9 | { 10 | class DivExpression : RawExpression 11 | { 12 | public RawExpression Left { get; set; } 13 | public RawExpression Right { get; set; } 14 | 15 | public override double Execute(Dictionary variables) 16 | { 17 | return this.Left.Execute(variables) / this.Right.Execute(variables); 18 | } 19 | 20 | public override RawExpression Apply(Dictionary variables) 21 | { 22 | return new DivExpression 23 | { 24 | Left = this.Left.Apply(variables), 25 | Right = this.Right.Apply(variables), 26 | }; 27 | } 28 | 29 | public override RawExpression Different(string variable) 30 | { 31 | return new DivExpression 32 | { 33 | Left = new SubExpression 34 | { 35 | Left = new MulExpression 36 | { 37 | Left = this.Left.Different(variable), 38 | Right = this.Right, 39 | }, 40 | Right = new MulExpression 41 | { 42 | Left = this.Left, 43 | Right = this.Right.Different(variable), 44 | }, 45 | }, 46 | Right = new PowerExpression 47 | { 48 | Left = this.Right, 49 | Right = new NumberExpression 50 | { 51 | Number = 2, 52 | }, 53 | }, 54 | }; 55 | } 56 | 57 | public override bool ContainsVariable(string variable) 58 | { 59 | return this.Left.ContainsVariable(variable) || this.Right.ContainsVariable(variable); 60 | } 61 | 62 | public override RawExpression SimplifyInternal() 63 | { 64 | RawExpression sleft = this.Left.Simplify(); 65 | RawExpression sright = this.Right.Simplify(); 66 | NumberExpression nleft = sleft as NumberExpression; 67 | NumberExpression nright = sright as NumberExpression; 68 | if (nleft != null) 69 | { 70 | if (nleft.Number == 0) 71 | { 72 | return new NumberExpression 73 | { 74 | Number = 0, 75 | }; 76 | } 77 | } 78 | else if (nright != null) 79 | { 80 | if (nright.Number == 1) 81 | { 82 | return sleft; 83 | } 84 | } 85 | return new DivExpression 86 | { 87 | Left = sleft, 88 | Right = sright, 89 | }; 90 | } 91 | 92 | public override Expression CompileInternal(Dictionary parameters) 93 | { 94 | return Expression.Divide(this.Left.CompileInternal(parameters), this.Right.CompileInternal(parameters)); 95 | } 96 | 97 | public override string ToCode() 98 | { 99 | return "(" + this.Left.ToCode() + " / " + this.Right.ToCode() + ")"; 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/OperatorExpressions/MulExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.PrimitiveExpressions; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation.OperatorExpressions 9 | { 10 | class MulExpression : RawExpression 11 | { 12 | public RawExpression Left { get; set; } 13 | public RawExpression Right { get; set; } 14 | 15 | public override double Execute(Dictionary variables) 16 | { 17 | return this.Left.Execute(variables) * this.Right.Execute(variables); 18 | } 19 | 20 | public override RawExpression Apply(Dictionary variables) 21 | { 22 | return new MulExpression 23 | { 24 | Left = this.Left.Apply(variables), 25 | Right = this.Right.Apply(variables), 26 | }; 27 | } 28 | 29 | public override RawExpression Different(string variable) 30 | { 31 | return new AddExpression 32 | { 33 | Left = new MulExpression 34 | { 35 | Left = this.Left.Different(variable), 36 | Right = this.Right, 37 | }, 38 | Right = new MulExpression 39 | { 40 | Left = this.Left, 41 | Right = this.Right.Different(variable), 42 | }, 43 | }; 44 | } 45 | 46 | public override bool ContainsVariable(string variable) 47 | { 48 | return this.Left.ContainsVariable(variable) || this.Right.ContainsVariable(variable); 49 | } 50 | 51 | public override RawExpression SimplifyInternal() 52 | { 53 | RawExpression sleft = this.Left.Simplify(); 54 | RawExpression sright = this.Right.Simplify(); 55 | NumberExpression nleft = sleft as NumberExpression; 56 | NumberExpression nright = sright as NumberExpression; 57 | if (nleft != null) 58 | { 59 | if (nleft.Number == 0) 60 | { 61 | return new NumberExpression 62 | { 63 | Number = 0, 64 | }; 65 | } 66 | else if (nleft.Number == 1) 67 | { 68 | return sright; 69 | } 70 | } 71 | else if (nright != null) 72 | { 73 | if (nright.Number == 0) 74 | { 75 | return new NumberExpression 76 | { 77 | Number = 0, 78 | }; 79 | } 80 | else if (nright.Number == 1) 81 | { 82 | return sleft; 83 | } 84 | } 85 | return new MulExpression 86 | { 87 | Left = sleft, 88 | Right = sright, 89 | }; 90 | } 91 | 92 | public override Expression CompileInternal(Dictionary parameters) 93 | { 94 | return Expression.Multiply(this.Left.CompileInternal(parameters), this.Right.CompileInternal(parameters)); 95 | } 96 | 97 | public override string ToCode() 98 | { 99 | return "(" + this.Left.ToCode() + " * " + this.Right.ToCode() + ")"; 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/OperatorExpressions/NegExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.PrimitiveExpressions; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation.OperatorExpressions 9 | { 10 | class NegExpression : RawExpression 11 | { 12 | public RawExpression Op { get; set; } 13 | 14 | public override double Execute(Dictionary variables) 15 | { 16 | return -this.Op.Execute(variables); 17 | } 18 | 19 | public override RawExpression Apply(Dictionary variables) 20 | { 21 | return new NegExpression 22 | { 23 | Op = this.Op.Apply(variables), 24 | }; 25 | } 26 | 27 | public override RawExpression Different(string variable) 28 | { 29 | return new NegExpression 30 | { 31 | Op = this.Op.Different(variable), 32 | }; 33 | } 34 | 35 | public override bool ContainsVariable(string variable) 36 | { 37 | return this.Op.ContainsVariable(variable); 38 | } 39 | 40 | public override RawExpression SimplifyInternal() 41 | { 42 | return new NegExpression 43 | { 44 | Op = this.Op.Simplify(), 45 | }; 46 | } 47 | 48 | public override Expression CompileInternal(Dictionary parameters) 49 | { 50 | return Expression.Negate(this.Op.CompileInternal(parameters)); 51 | } 52 | 53 | public override string ToCode() 54 | { 55 | return "(-" + this.Op.ToCode() + ")"; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/OperatorExpressions/PowerExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.PrimitiveExpressions; 6 | using FvCalculation.FunctionExpressions; 7 | using System.Linq.Expressions; 8 | using System.Reflection; 9 | 10 | namespace FvCalculation.OperatorExpressions 11 | { 12 | class PowerExpression : RawExpression 13 | { 14 | public RawExpression Left { get; set; } 15 | public RawExpression Right { get; set; } 16 | 17 | private static double Calculate(double a, double b) 18 | { 19 | double ib = (int)Math.Round(b); 20 | if (b >= 0 && -RawExpression.ZeroNumber <= (ib - b) && (ib - b) <= RawExpression.ZeroNumber) 21 | { 22 | if (ib == 0) 23 | { 24 | return 1; 25 | } 26 | else 27 | { 28 | double result = 1; 29 | for (int i = 0; i < ib; i++) 30 | { 31 | result *= a; 32 | } 33 | return result; 34 | } 35 | } 36 | else 37 | { 38 | return Math.Exp(b * Math.Log(a)); 39 | } 40 | } 41 | 42 | public override double Execute(Dictionary variables) 43 | { 44 | double a = this.Left.Execute(variables); 45 | double b = this.Right.Execute(variables); 46 | return Calculate(a, b); 47 | } 48 | 49 | public override RawExpression Apply(Dictionary variables) 50 | { 51 | return new PowerExpression 52 | { 53 | Left = this.Left.Apply(variables), 54 | Right = this.Right.Apply(variables), 55 | }; 56 | } 57 | 58 | public override RawExpression Different(string variable) 59 | { 60 | bool lf = this.Left.ContainsVariable(variable); 61 | bool rf = this.Right.ContainsVariable(variable); 62 | if (lf) 63 | { 64 | if (rf) 65 | { 66 | return new ExpExpression 67 | { 68 | Op = new MulExpression 69 | { 70 | Left = this.Right, 71 | Right = new LnExpression 72 | { 73 | Op = this.Left, 74 | }, 75 | }, 76 | }.Different(variable); 77 | } 78 | else 79 | { 80 | return new MulExpression 81 | { 82 | Left = this.Right, 83 | Right = new PowerExpression 84 | { 85 | Left = this.Left, 86 | Right = new SubExpression 87 | { 88 | Left = this.Right, 89 | Right = new NumberExpression 90 | { 91 | Number = 1, 92 | }, 93 | }, 94 | }, 95 | }; 96 | } 97 | } 98 | else 99 | { 100 | if (rf) 101 | { 102 | return new MulExpression 103 | { 104 | Left = this, 105 | Right = new LnExpression 106 | { 107 | Op = this.Left, 108 | }, 109 | }; 110 | } 111 | else 112 | { 113 | return new NumberExpression 114 | { 115 | Number = 0, 116 | }; 117 | } 118 | } 119 | } 120 | 121 | public override bool ContainsVariable(string variable) 122 | { 123 | return this.Left.ContainsVariable(variable) || this.Right.ContainsVariable(variable); 124 | } 125 | 126 | public override RawExpression SimplifyInternal() 127 | { 128 | RawExpression sleft = this.Left.Simplify(); 129 | RawExpression sright = this.Right.Simplify(); 130 | NumberExpression nleft = sleft as NumberExpression; 131 | NumberExpression nright = sright as NumberExpression; 132 | if (nright != null) 133 | { 134 | if (nright.Number == 0) 135 | { 136 | return new NumberExpression 137 | { 138 | Number = 1, 139 | }; 140 | } 141 | else if (nright.Number == 1) 142 | { 143 | return sleft; 144 | } 145 | } 146 | else if (nleft != null) 147 | { 148 | if (nleft.Number == 0) 149 | { 150 | return new NumberExpression 151 | { 152 | Number = 0, 153 | }; 154 | } 155 | else if (nleft.Number == 1) 156 | { 157 | return new NumberExpression 158 | { 159 | Number = 1, 160 | }; 161 | } 162 | } 163 | return new PowerExpression 164 | { 165 | Left = sleft, 166 | Right = sright, 167 | }; 168 | } 169 | 170 | public override Expression CompileInternal(Dictionary parameters) 171 | { 172 | MethodInfo methodInfo = typeof(PowerExpression).GetMethod("Calculate", BindingFlags.NonPublic | BindingFlags.Static); 173 | return Expression.Call(methodInfo, this.Left.CompileInternal(parameters), this.Right.CompileInternal(parameters)); 174 | } 175 | 176 | public override string ToCode() 177 | { 178 | return "pow(" + this.Left.ToCode() + ", " + this.Right.ToCode() + ")"; 179 | } 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/OperatorExpressions/SubExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.PrimitiveExpressions; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation.OperatorExpressions 9 | { 10 | class SubExpression : RawExpression 11 | { 12 | public RawExpression Left { get; set; } 13 | public RawExpression Right { get; set; } 14 | 15 | public override double Execute(Dictionary variables) 16 | { 17 | return this.Left.Execute(variables) - this.Right.Execute(variables); 18 | } 19 | 20 | public override RawExpression Apply(Dictionary variables) 21 | { 22 | return new SubExpression 23 | { 24 | Left = this.Left.Apply(variables), 25 | Right = this.Right.Apply(variables), 26 | }; 27 | } 28 | 29 | public override RawExpression Different(string variable) 30 | { 31 | return new SubExpression 32 | { 33 | Left = this.Left.Different(variable), 34 | Right = this.Right.Different(variable), 35 | }; 36 | } 37 | 38 | public override bool ContainsVariable(string variable) 39 | { 40 | return this.Left.ContainsVariable(variable) || this.Right.ContainsVariable(variable); 41 | } 42 | 43 | public override RawExpression SimplifyInternal() 44 | { 45 | RawExpression sleft = this.Left.Simplify(); 46 | RawExpression sright = this.Right.Simplify(); 47 | NumberExpression nleft = sleft as NumberExpression; 48 | NumberExpression nright = sright as NumberExpression; 49 | if (nleft != null && nleft.Number == 0) 50 | { 51 | return new NegExpression 52 | { 53 | Op = sright, 54 | }; 55 | } 56 | else if (nright != null && nright.Number == 0) 57 | { 58 | return sleft; 59 | } 60 | else 61 | { 62 | return new SubExpression 63 | { 64 | Left = sleft, 65 | Right = sright, 66 | }; 67 | } 68 | } 69 | 70 | public override Expression CompileInternal(Dictionary parameters) 71 | { 72 | return Expression.Subtract(this.Left.CompileInternal(parameters), this.Right.CompileInternal(parameters)); 73 | } 74 | 75 | public override string ToCode() 76 | { 77 | return "(" + this.Left.ToCode() + " - " + this.Right.ToCode() + ")"; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/PrimitiveExpressions/NumberExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Globalization; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation.PrimitiveExpressions 9 | { 10 | class NumberExpression : RawExpression 11 | { 12 | public double Number { get; set; } 13 | 14 | public override double Execute(Dictionary variables) 15 | { 16 | return this.Number; 17 | } 18 | 19 | public override RawExpression Apply(Dictionary variables) 20 | { 21 | return this; 22 | } 23 | 24 | public override RawExpression Different(string variable) 25 | { 26 | return new NumberExpression 27 | { 28 | Number = 0, 29 | }; 30 | } 31 | 32 | public override bool ContainsVariable(string variable) 33 | { 34 | return false; 35 | } 36 | 37 | public override RawExpression SimplifyInternal() 38 | { 39 | return this; 40 | } 41 | 42 | public override Expression CompileInternal(Dictionary parameters) 43 | { 44 | return Expression.Constant(this.Number); 45 | } 46 | 47 | public override string ToCode() 48 | { 49 | return "_F(" + this.Number.ToString("#.0", CultureInfo.InvariantCulture) + ")"; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/PrimitiveExpressions/VariableExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Linq.Expressions; 6 | 7 | namespace FvCalculation.PrimitiveExpressions 8 | { 9 | class VariableExpression : RawExpression 10 | { 11 | public string Name { get; set; } 12 | 13 | public override double Execute(Dictionary variables) 14 | { 15 | return variables[this.Name]; 16 | } 17 | 18 | public override RawExpression Apply(Dictionary variables) 19 | { 20 | double value = 0; 21 | if (variables.TryGetValue(this.Name, out value)) 22 | { 23 | return new NumberExpression 24 | { 25 | Number = value, 26 | }; 27 | } 28 | else 29 | { 30 | return this; 31 | } 32 | } 33 | 34 | public override RawExpression Different(string variable) 35 | { 36 | if (this.Name == variable) 37 | { 38 | return new NumberExpression 39 | { 40 | Number = 1, 41 | }; 42 | } 43 | else 44 | { 45 | return new NumberExpression 46 | { 47 | Number = 0, 48 | }; 49 | } 50 | } 51 | 52 | public override bool ContainsVariable(string variable) 53 | { 54 | return this.Name == variable; 55 | } 56 | 57 | public override RawExpression SimplifyInternal() 58 | { 59 | return this; 60 | } 61 | 62 | public override Expression CompileInternal(Dictionary parameters) 63 | { 64 | return parameters[this.Name]; 65 | } 66 | 67 | public override string ToCode() 68 | { 69 | return this.Name; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("FvCalculation")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("FvCalculation")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("239cbb4a-866a-4dd4-96cd-b06718141738")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvCalculation/RawExpression.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation.PrimitiveExpressions; 6 | using System.Linq.Expressions; 7 | 8 | namespace FvCalculation 9 | { 10 | public abstract class RawExpression 11 | { 12 | public const double ZeroNumber = 0.000001; 13 | 14 | public static RawExpression Parse(string s) 15 | { 16 | return ExpressionParser.Parse(s); 17 | } 18 | 19 | public abstract double Execute(Dictionary variables); 20 | public abstract RawExpression Apply(Dictionary variables); 21 | public abstract RawExpression Different(string variable); 22 | public abstract bool ContainsVariable(string variable); 23 | public abstract RawExpression SimplifyInternal(); 24 | public abstract Expression CompileInternal(Dictionary parameters); 25 | public abstract string ToCode(); 26 | 27 | public RawExpression Simplify() 28 | { 29 | try 30 | { 31 | return new NumberExpression 32 | { 33 | Number = Execute(new Dictionary()), 34 | }; 35 | } 36 | catch (KeyNotFoundException) 37 | { 38 | RawExpression s = SimplifyInternal(); 39 | try 40 | { 41 | return new NumberExpression 42 | { 43 | Number = s.Execute(new Dictionary()), 44 | }; 45 | } 46 | catch (KeyNotFoundException) 47 | { 48 | return s; 49 | } 50 | } 51 | } 52 | 53 | public Func Compile(string variable) 54 | { 55 | Dictionary parameters = new Dictionary(); 56 | ParameterExpression parameter = Expression.Parameter(typeof(double), variable); 57 | parameters.Add(variable, parameter); 58 | 59 | return Expression.Lambda>( 60 | CompileInternal(parameters), 61 | parameter 62 | ) 63 | .Compile(); 64 | } 65 | 66 | public override string ToString() 67 | { 68 | return ToCode(); 69 | } 70 | } 71 | 72 | public class FunctionNameAttribute : Attribute 73 | { 74 | public string Name { get; set; } 75 | 76 | public FunctionNameAttribute(string name) 77 | { 78 | this.Name = name; 79 | } 80 | }; 81 | 82 | public abstract class FunctionExpression : RawExpression 83 | { 84 | private static Dictionary functionExpressionTypes = null; 85 | 86 | private string name = null; 87 | 88 | public RawExpression Op { get; set; } 89 | 90 | public string Name 91 | { 92 | get 93 | { 94 | if (this.name == null) 95 | { 96 | this.name = ((FunctionNameAttribute)this.GetType().GetCustomAttributes(typeof(FunctionNameAttribute), false).First()).Name; 97 | } 98 | return this.name; 99 | } 100 | } 101 | 102 | public override RawExpression Apply(Dictionary variables) 103 | { 104 | FunctionExpression f = (FunctionExpression)this.GetType().GetConstructor(new Type[] { }).Invoke(new object[] { }); 105 | f.Op = this.Op.Apply(variables); 106 | return f; 107 | } 108 | 109 | public override bool ContainsVariable(string variable) 110 | { 111 | return this.Op.ContainsVariable(variable); 112 | } 113 | 114 | public override RawExpression SimplifyInternal() 115 | { 116 | FunctionExpression f = (FunctionExpression)this.GetType().GetConstructor(new Type[] { }).Invoke(new object[] { }); 117 | f.Op = this.Op.Simplify(); 118 | return f; 119 | } 120 | 121 | public override string ToCode() 122 | { 123 | return this.Name + "(" + this.Op.ToCode() + ")"; 124 | } 125 | 126 | public static FunctionExpression FromName(string name) 127 | { 128 | if (functionExpressionTypes == null) 129 | { 130 | functionExpressionTypes = new Dictionary(); 131 | foreach (var type in typeof(FunctionExpression).Assembly.GetTypes()) 132 | { 133 | if (!type.IsAbstract && typeof(FunctionExpression).IsAssignableFrom(type)) 134 | { 135 | FunctionNameAttribute att = (FunctionNameAttribute)type.GetCustomAttributes(typeof(FunctionNameAttribute), false).First(); 136 | functionExpressionTypes.Add(att.Name, type); 137 | } 138 | } 139 | } 140 | Type funcType = null; 141 | if (functionExpressionTypes.TryGetValue(name, out funcType)) 142 | { 143 | return (FunctionExpression)funcType.GetConstructor(new Type[] { }).Invoke(new object[] { }); 144 | } 145 | else 146 | { 147 | return null; 148 | } 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/FvGUI.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {C5F3E283-2745-42EC-BD8B-1BAC88202164} 9 | WinExe 10 | Properties 11 | FvGUI 12 | FvGUI 13 | v4.0 14 | Client 15 | 512 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | x86 27 | true 28 | full 29 | false 30 | bin\Debug\ 31 | DEBUG;TRACE 32 | prompt 33 | 4 34 | true 35 | 36 | 37 | x86 38 | pdbonly 39 | true 40 | bin\Release\ 41 | TRACE 42 | prompt 43 | 4 44 | true 45 | 46 | 47 | 48 | ..\Cloo.dll 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | Form 67 | 68 | 69 | MainForm.cs 70 | 71 | 72 | Form 73 | 74 | 75 | Form 76 | 77 | 78 | OpenClSetting.cs 79 | 80 | 81 | 82 | 83 | MainForm.cs 84 | 85 | 86 | OpenClSetting.cs 87 | 88 | 89 | ResXFileCodeGenerator 90 | Resources.Designer.cs 91 | Designer 92 | 93 | 94 | True 95 | Resources.resx 96 | True 97 | 98 | 99 | 100 | SettingsSingleFileGenerator 101 | Settings.Designer.cs 102 | 103 | 104 | True 105 | Settings.settings 106 | True 107 | 108 | 109 | 110 | 111 | {F21E4754-8EBB-41E7-A2F8-12D68B0EF7A4} 112 | FvCalculation 113 | 114 | 115 | 116 | 123 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/GpuHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Cloo; 6 | using System.Diagnostics; 7 | using System.Collections.ObjectModel; 8 | using System.Runtime.InteropServices; 9 | 10 | 11 | namespace FvGUI 12 | { 13 | class GpuHelper : FvGUI.IGpuHelper where TFP : struct 14 | { 15 | private ComputeContext context; 16 | private ComputeCommandQueue commands; 17 | ICollection events = new Collection(); 18 | 19 | FPType fpType; 20 | 21 | public GpuHelper(ComputeContext context, FPType fptype) 22 | { 23 | this.context = context; 24 | commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); 25 | this.fpType = fptype; 26 | } 27 | 28 | public void FillPoints(int imageWidth, int imageHeight, int unit, double originx, double originy, string funcCode, string funcdxCode, string funcdyCode, double[, ,] points) 29 | { 30 | var gpuResult = Compute(imageWidth, imageHeight, unit, (float)originx, (float)originy, funcCode, funcdxCode, funcdyCode); 31 | 32 | int w = imageWidth - 2; 33 | int h = imageHeight - 2; 34 | int cx = imageWidth / 2; 35 | int cy = imageHeight / 2; 36 | 37 | for (int y = 1; y <= h; y++) 38 | { 39 | double py = originy + (double)(y - cy) / unit; 40 | for (int x = 1; x <= w; x++) 41 | { 42 | double px = originx + (double)(cx - x) / unit; 43 | 44 | int loc = (y - 1) * w + (x - 1); 45 | points[y - 1, x - 1, 0] = Convert.ToDouble(gpuResult.Item1[loc]); 46 | points[y - 1, x - 1, 1] = py; 47 | points[y - 1, x - 1, 2] = px; 48 | points[y - 1, x - 1, 3] = Convert.ToDouble(gpuResult.Item2[loc]); 49 | } 50 | } 51 | } 52 | 53 | private Tuple Compute(int imageWidth, int imageHeight, int unit, float originx, float originy, string funcCode, string funcdxCode, string funcdyCode) 54 | { 55 | 56 | int w = imageWidth - 2; 57 | int h = imageHeight - 2; 58 | int cx = imageWidth / 2; 59 | int cy = imageHeight / 2; 60 | 61 | int bufferSize = w * h; 62 | 63 | ComputeBuffer points = new ComputeBuffer(context, ComputeMemoryFlags.WriteOnly, bufferSize); 64 | 65 | string source = Encoding.ASCII.GetString(Properties.Resources.function_vis); 66 | 67 | source = source.Replace("{func}", funcCode); 68 | source = source.Replace("{dfuncdx}", funcdxCode); 69 | source = source.Replace("{dfuncdy}", funcdyCode); 70 | 71 | if (fpType == FPType.FP64AMD) 72 | { 73 | source = "#define AMDFP64\n" + source; 74 | } 75 | else if (fpType == FPType.FP64) 76 | { 77 | source = "#define FP64\n" + source; 78 | } 79 | 80 | ComputeProgram program = new ComputeProgram(context, source); 81 | try 82 | { 83 | program.Build(null, null, null, IntPtr.Zero); 84 | } 85 | catch (Exception) 86 | { 87 | var log = program.GetBuildLog(context.Devices[0]); 88 | Debugger.Break(); 89 | } 90 | 91 | ComputeKernel kernelx = program.CreateKernel("ComputeX"); 92 | ComputeKernel kernely = program.CreateKernel("ComputeY"); 93 | 94 | TFP[] pointsArrayX = RunKernal(unit, w, h, cx, cy, originx, originy, bufferSize, points, kernelx); 95 | TFP[] pointsArrayY = RunKernal(unit, w, h, cx, cy, originx, originy, bufferSize, points, kernely); 96 | 97 | kernelx.Dispose(); 98 | kernely.Dispose(); 99 | program.Dispose(); 100 | points.Dispose(); 101 | 102 | return Tuple.Create(pointsArrayX, pointsArrayY); 103 | } 104 | 105 | private TFP[] RunKernal(int unit, int w, int h, int cx, int cy, float originx, float originy, int bufferSize, ComputeBuffer points, ComputeKernel kernel) 106 | { 107 | kernel.SetMemoryArgument(0, points); 108 | kernel.SetValueArgument(1, unit); 109 | kernel.SetValueArgument(2, w); 110 | kernel.SetValueArgument(3, cx); 111 | kernel.SetValueArgument(4, cy); 112 | kernel.SetValueArgument(5, originx); 113 | kernel.SetValueArgument(6, originy); 114 | 115 | // BUG: ATI Stream v2.2 crash if event list not null. 116 | commands.Execute(kernel, null, new long[] { w, h }, null, events); 117 | //commands.Execute(kernel, null, new long[] { count }, null, null); 118 | 119 | TFP[] pointsArray = new TFP[bufferSize]; 120 | GCHandle arrCHandle = GCHandle.Alloc(pointsArray, GCHandleType.Pinned); 121 | 122 | commands.Read(points, true, 0, bufferSize, arrCHandle.AddrOfPinnedObject(), events); 123 | 124 | arrCHandle.Free(); 125 | return pointsArray; 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/GpuHelperFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using Cloo; 6 | 7 | namespace FvGUI 8 | { 9 | enum FPType 10 | { 11 | Single, 12 | FP64, 13 | FP64AMD 14 | } 15 | 16 | static class GpuHelperFactory 17 | { 18 | 19 | public static IGpuHelper CreateHelper(ComputePlatform platform, ComputeDevice device, FPType fptype) 20 | { 21 | ComputeContextPropertyList properties = new ComputeContextPropertyList(platform); 22 | var context = new ComputeContext(new[] { device }, properties, null, IntPtr.Zero); 23 | 24 | if (fptype == FPType.Single) 25 | { 26 | return new GpuHelper(context, fptype); 27 | } 28 | else 29 | { 30 | return new GpuHelper(context, fptype); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/IGpuHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace FvGUI 3 | { 4 | interface IGpuHelper 5 | { 6 | void FillPoints(int imageWidth, int imageHeight, int unit, double originx, double originy, string funcCode, string funcdxCode, string funcdyCode, double[, ,] points); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/MainForm.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace FvGUI 2 | { 3 | partial class MainForm 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.tableLayoutPanelMain = new System.Windows.Forms.TableLayoutPanel(); 32 | this.label1 = new System.Windows.Forms.Label(); 33 | this.label2 = new System.Windows.Forms.Label(); 34 | this.label3 = new System.Windows.Forms.Label(); 35 | this.label4 = new System.Windows.Forms.Label(); 36 | this.textBoxUnitPixels = new System.Windows.Forms.TextBox(); 37 | this.textBoxOriginX = new System.Windows.Forms.TextBox(); 38 | this.textBoxOriginY = new System.Windows.Forms.TextBox(); 39 | this.textBoxFunction = new System.Windows.Forms.TextBox(); 40 | this.buttonRender = new System.Windows.Forms.Button(); 41 | this.labelErrorMessage = new System.Windows.Forms.Label(); 42 | this.panelImage = new System.Windows.Forms.Panel(); 43 | this.tableLayoutPanelMain.SuspendLayout(); 44 | this.SuspendLayout(); 45 | // 46 | // tableLayoutPanelMain 47 | // 48 | this.tableLayoutPanelMain.ColumnCount = 6; 49 | this.tableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 50 | this.tableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F)); 51 | this.tableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 52 | this.tableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F)); 53 | this.tableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 54 | this.tableLayoutPanelMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F)); 55 | this.tableLayoutPanelMain.Controls.Add(this.label1, 0, 0); 56 | this.tableLayoutPanelMain.Controls.Add(this.label2, 0, 1); 57 | this.tableLayoutPanelMain.Controls.Add(this.label3, 2, 1); 58 | this.tableLayoutPanelMain.Controls.Add(this.label4, 4, 1); 59 | this.tableLayoutPanelMain.Controls.Add(this.textBoxUnitPixels, 1, 1); 60 | this.tableLayoutPanelMain.Controls.Add(this.textBoxOriginX, 3, 1); 61 | this.tableLayoutPanelMain.Controls.Add(this.textBoxOriginY, 5, 1); 62 | this.tableLayoutPanelMain.Controls.Add(this.textBoxFunction, 1, 0); 63 | this.tableLayoutPanelMain.Controls.Add(this.buttonRender, 0, 2); 64 | this.tableLayoutPanelMain.Controls.Add(this.labelErrorMessage, 1, 2); 65 | this.tableLayoutPanelMain.Controls.Add(this.panelImage, 0, 3); 66 | this.tableLayoutPanelMain.Dock = System.Windows.Forms.DockStyle.Fill; 67 | this.tableLayoutPanelMain.Location = new System.Drawing.Point(0, 0); 68 | this.tableLayoutPanelMain.Name = "tableLayoutPanelMain"; 69 | this.tableLayoutPanelMain.RowCount = 4; 70 | this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle()); 71 | this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle()); 72 | this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle()); 73 | this.tableLayoutPanelMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 74 | this.tableLayoutPanelMain.Size = new System.Drawing.Size(707, 657); 75 | this.tableLayoutPanelMain.TabIndex = 0; 76 | // 77 | // label1 78 | // 79 | this.label1.Dock = System.Windows.Forms.DockStyle.Fill; 80 | this.label1.Location = new System.Drawing.Point(3, 0); 81 | this.label1.Name = "label1"; 82 | this.label1.Size = new System.Drawing.Size(100, 29); 83 | this.label1.TabIndex = 0; 84 | this.label1.Text = "&F(x, y)=0:"; 85 | this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 86 | // 87 | // label2 88 | // 89 | this.label2.Dock = System.Windows.Forms.DockStyle.Fill; 90 | this.label2.Location = new System.Drawing.Point(3, 29); 91 | this.label2.Name = "label2"; 92 | this.label2.Size = new System.Drawing.Size(100, 29); 93 | this.label2.TabIndex = 2; 94 | this.label2.Text = "&Unit Pixels:"; 95 | this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 96 | // 97 | // label3 98 | // 99 | this.label3.Dock = System.Windows.Forms.DockStyle.Fill; 100 | this.label3.Location = new System.Drawing.Point(238, 29); 101 | this.label3.Name = "label3"; 102 | this.label3.Size = new System.Drawing.Size(100, 29); 103 | this.label3.TabIndex = 4; 104 | this.label3.Text = "Origin &X"; 105 | this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 106 | // 107 | // label4 108 | // 109 | this.label4.Dock = System.Windows.Forms.DockStyle.Fill; 110 | this.label4.Location = new System.Drawing.Point(473, 29); 111 | this.label4.Name = "label4"; 112 | this.label4.Size = new System.Drawing.Size(100, 29); 113 | this.label4.TabIndex = 6; 114 | this.label4.Text = "Origin &Y"; 115 | this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 116 | // 117 | // textBoxUnitPixels 118 | // 119 | this.textBoxUnitPixels.Dock = System.Windows.Forms.DockStyle.Fill; 120 | this.textBoxUnitPixels.Location = new System.Drawing.Point(109, 32); 121 | this.textBoxUnitPixels.Name = "textBoxUnitPixels"; 122 | this.textBoxUnitPixels.Size = new System.Drawing.Size(123, 20); 123 | this.textBoxUnitPixels.TabIndex = 3; 124 | this.textBoxUnitPixels.Text = "100"; 125 | this.textBoxUnitPixels.TextChanged += new System.EventHandler(this.textBoxUnitPixels_TextChanged); 126 | // 127 | // textBoxOriginX 128 | // 129 | this.textBoxOriginX.Dock = System.Windows.Forms.DockStyle.Fill; 130 | this.textBoxOriginX.Location = new System.Drawing.Point(344, 32); 131 | this.textBoxOriginX.Name = "textBoxOriginX"; 132 | this.textBoxOriginX.Size = new System.Drawing.Size(123, 20); 133 | this.textBoxOriginX.TabIndex = 5; 134 | this.textBoxOriginX.Text = "0"; 135 | this.textBoxOriginX.TextChanged += new System.EventHandler(this.textBoxOriginX_TextChanged); 136 | // 137 | // textBoxOriginY 138 | // 139 | this.textBoxOriginY.Dock = System.Windows.Forms.DockStyle.Fill; 140 | this.textBoxOriginY.Location = new System.Drawing.Point(579, 32); 141 | this.textBoxOriginY.Name = "textBoxOriginY"; 142 | this.textBoxOriginY.Size = new System.Drawing.Size(125, 20); 143 | this.textBoxOriginY.TabIndex = 7; 144 | this.textBoxOriginY.Text = "0"; 145 | this.textBoxOriginY.TextChanged += new System.EventHandler(this.textBoxOriginY_TextChanged); 146 | // 147 | // textBoxFunction 148 | // 149 | this.tableLayoutPanelMain.SetColumnSpan(this.textBoxFunction, 5); 150 | this.textBoxFunction.Dock = System.Windows.Forms.DockStyle.Fill; 151 | this.textBoxFunction.Location = new System.Drawing.Point(109, 3); 152 | this.textBoxFunction.Name = "textBoxFunction"; 153 | this.textBoxFunction.Size = new System.Drawing.Size(595, 20); 154 | this.textBoxFunction.TabIndex = 1; 155 | this.textBoxFunction.Text = "x-y"; 156 | this.textBoxFunction.TextChanged += new System.EventHandler(this.textBoxFunction_TextChanged); 157 | // 158 | // buttonRender 159 | // 160 | this.buttonRender.Dock = System.Windows.Forms.DockStyle.Fill; 161 | this.buttonRender.Location = new System.Drawing.Point(3, 61); 162 | this.buttonRender.Name = "buttonRender"; 163 | this.buttonRender.Size = new System.Drawing.Size(100, 25); 164 | this.buttonRender.TabIndex = 8; 165 | this.buttonRender.Text = "&Render"; 166 | this.buttonRender.UseVisualStyleBackColor = true; 167 | this.buttonRender.Click += new System.EventHandler(this.buttonRender_Click); 168 | // 169 | // labelErrorMessage 170 | // 171 | this.labelErrorMessage.AutoSize = true; 172 | this.tableLayoutPanelMain.SetColumnSpan(this.labelErrorMessage, 5); 173 | this.labelErrorMessage.Dock = System.Windows.Forms.DockStyle.Fill; 174 | this.labelErrorMessage.Location = new System.Drawing.Point(109, 58); 175 | this.labelErrorMessage.Name = "labelErrorMessage"; 176 | this.labelErrorMessage.Size = new System.Drawing.Size(595, 31); 177 | this.labelErrorMessage.TabIndex = 9; 178 | this.labelErrorMessage.Text = "(Ready)"; 179 | this.labelErrorMessage.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 180 | // 181 | // panelImage 182 | // 183 | this.tableLayoutPanelMain.SetColumnSpan(this.panelImage, 6); 184 | this.panelImage.Dock = System.Windows.Forms.DockStyle.Fill; 185 | this.panelImage.Location = new System.Drawing.Point(3, 92); 186 | this.panelImage.Name = "panelImage"; 187 | this.panelImage.Size = new System.Drawing.Size(701, 562); 188 | this.panelImage.TabIndex = 10; 189 | this.panelImage.SizeChanged += new System.EventHandler(this.panelImage_SizeChanged); 190 | this.panelImage.Paint += new System.Windows.Forms.PaintEventHandler(this.panelImage_Paint); 191 | // 192 | // MainForm 193 | // 194 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 195 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 196 | this.ClientSize = new System.Drawing.Size(707, 657); 197 | this.Controls.Add(this.tableLayoutPanelMain); 198 | this.Name = "MainForm"; 199 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 200 | this.Text = "Function Visualizer v1.0 GPU Accelerated"; 201 | this.Shown += new System.EventHandler(this.MainForm_Shown); 202 | this.tableLayoutPanelMain.ResumeLayout(false); 203 | this.tableLayoutPanelMain.PerformLayout(); 204 | this.ResumeLayout(false); 205 | 206 | } 207 | 208 | #endregion 209 | 210 | private System.Windows.Forms.TableLayoutPanel tableLayoutPanelMain; 211 | private System.Windows.Forms.Label label1; 212 | private System.Windows.Forms.Label label2; 213 | private System.Windows.Forms.Label label3; 214 | private System.Windows.Forms.Label label4; 215 | private System.Windows.Forms.TextBox textBoxUnitPixels; 216 | private System.Windows.Forms.TextBox textBoxOriginX; 217 | private System.Windows.Forms.TextBox textBoxOriginY; 218 | private System.Windows.Forms.TextBox textBoxFunction; 219 | private System.Windows.Forms.Button buttonRender; 220 | private System.Windows.Forms.Label labelErrorMessage; 221 | private System.Windows.Forms.Panel panelImage; 222 | } 223 | } 224 | 225 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/MainForm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Windows.Forms; 9 | using FvCalculation; 10 | using System.Reflection; 11 | using System.Globalization; 12 | using System.Drawing.Imaging; 13 | using System.Threading; 14 | 15 | namespace FvGUI 16 | { 17 | /* 18 | * exp(sin(x)+cos(y)) - sin(exp(x+y)) 19 | * sin(sin(x)+cos(y)) - cos(sin(x*y)+cos(x)) 20 | * sin(x^2+y^2) - cos(x*y) 21 | */ 22 | public partial class MainForm : Form 23 | { 24 | private const int SuggestedUnitPixel = 100; 25 | 26 | private RawExpression function = null; 27 | private int unitPixels = 0; 28 | private double originX = 0; 29 | private double originY = 0; 30 | private Bitmap imageBuffer = null; 31 | 32 | private IGpuHelper gpu; 33 | 34 | private void TextBoxChanged() 35 | { 36 | RawExpression tempFunction = null; 37 | int tempUnitPixels = 0; 38 | double tempOriginX = 0; 39 | double tempOriginY = 0; 40 | 41 | try 42 | { 43 | tempFunction = RawExpression.Parse(textBoxFunction.Text); 44 | } 45 | catch (Exception e) 46 | { 47 | buttonRender.Enabled = false; 48 | labelErrorMessage.Text = "[Function]" + e.Message; 49 | return; 50 | } 51 | 52 | try 53 | { 54 | tempUnitPixels = int.Parse(textBoxUnitPixels.Text, NumberStyles.None); 55 | } 56 | catch (Exception e) 57 | { 58 | buttonRender.Enabled = false; 59 | labelErrorMessage.Text = "[UnitPixels]" + e.Message; 60 | return; 61 | } 62 | 63 | try 64 | { 65 | tempOriginX = double.Parse(textBoxOriginX.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign); 66 | } 67 | catch (Exception e) 68 | { 69 | buttonRender.Enabled = false; 70 | labelErrorMessage.Text = "[OriginX]" + e.Message; 71 | return; 72 | } 73 | 74 | try 75 | { 76 | tempOriginY = double.Parse(textBoxOriginY.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign); 77 | } 78 | catch (Exception e) 79 | { 80 | buttonRender.Enabled = false; 81 | labelErrorMessage.Text = "[OriginY]" + e.Message; 82 | return; 83 | } 84 | 85 | this.function = tempFunction; 86 | this.unitPixels = tempUnitPixels; 87 | this.originX = tempOriginX; 88 | this.originY = tempOriginY; 89 | buttonRender.Enabled = true; 90 | labelErrorMessage.Text = "(Ready)"; 91 | } 92 | 93 | private void RebuildBuffer() 94 | { 95 | if (this.imageBuffer != null) 96 | { 97 | this.imageBuffer.Dispose(); 98 | } 99 | Size size = panelImage.Size; 100 | this.imageBuffer = new Bitmap(size.Width, size.Height); 101 | } 102 | 103 | private void RenderAxis() 104 | { 105 | if (this.unitPixels == 0) return; 106 | int w = this.imageBuffer.Width; 107 | int h = this.imageBuffer.Height; 108 | using (Graphics g = Graphics.FromImage(this.imageBuffer)) 109 | { 110 | g.FillRectangle(Brushes.White, 0, 0, w, h); 111 | 112 | int cx = (int)Math.Round(w / 2 - this.originX * this.unitPixels); 113 | int cy = (int)Math.Round(h / 2 + this.originY * this.unitPixels); 114 | int up = this.unitPixels; 115 | if (this.unitPixels < SuggestedUnitPixel) 116 | { 117 | up = this.unitPixels * (SuggestedUnitPixel / this.unitPixels); 118 | if (up < SuggestedUnitPixel) 119 | { 120 | up += this.unitPixels; 121 | } 122 | } 123 | else if (this.unitPixels > SuggestedUnitPixel) 124 | { 125 | up = this.unitPixels / (this.unitPixels / SuggestedUnitPixel); 126 | } 127 | double u = (double)up / this.unitPixels; 128 | 129 | int sx = cx - (cx / up * up); 130 | int sy = cy - (cy / up * up); 131 | for (int x = sx; x < w; x += up) 132 | { 133 | g.DrawLine((x == cx ? Pens.Black : Pens.LightGray), x, 0, x, h); 134 | g.DrawString(((x - cx) / up * u).ToString(), panelImage.Font, Brushes.Black, x, cy); 135 | } 136 | for (int y = sy; y < h; y += up) 137 | { 138 | g.DrawLine((y == cy ? Pens.Black : Pens.LightGray), 0, y, w, y); 139 | if (y != cy) 140 | { 141 | g.DrawString(((cy - y) / up * u).ToString(), panelImage.Font, Brushes.Black, cx, y); 142 | } 143 | } 144 | 145 | g.DrawRectangle(Pens.Black, 0, 0, w - 1, h - 1); 146 | } 147 | } 148 | 149 | private void Fill(BitmapData data, double x, double y, int cx, int cy, int w, int h) 150 | { 151 | int ix = (int)Math.Round((x - this.originX) * this.unitPixels) + cx; 152 | int iy = (int)Math.Round((this.originY - y) * this.unitPixels) + cy; 153 | if (1 <= ix && ix <= w && 1 <= iy && iy <= h) 154 | { 155 | unsafe 156 | { 157 | byte* color = (byte*)data.Scan0 + iy * data.Stride + ix * 3; 158 | color[0] = 255; 159 | color[1] = 0; 160 | color[2] = 0; 161 | } 162 | } 163 | } 164 | 165 | public MainForm() 166 | { 167 | InitializeComponent(); 168 | panelImage.GetType() 169 | .GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance) 170 | .SetValue(panelImage, true, new object[] { }); 171 | TextBoxChanged(); 172 | RebuildBuffer(); 173 | RenderAxis(); 174 | } 175 | 176 | private void buttonRender_Click(object sender, EventArgs e) 177 | { 178 | RenderAxis(); 179 | RenderAsnyc(RenderFunction); 180 | } 181 | 182 | private void textBoxFunction_TextChanged(object sender, EventArgs e) 183 | { 184 | TextBoxChanged(); 185 | } 186 | 187 | private void textBoxUnitPixels_TextChanged(object sender, EventArgs e) 188 | { 189 | TextBoxChanged(); 190 | } 191 | 192 | private void textBoxOriginX_TextChanged(object sender, EventArgs e) 193 | { 194 | TextBoxChanged(); 195 | } 196 | 197 | private void textBoxOriginY_TextChanged(object sender, EventArgs e) 198 | { 199 | TextBoxChanged(); 200 | } 201 | 202 | private void panelImage_Paint(object sender, PaintEventArgs e) 203 | { 204 | e.Graphics.DrawImage(this.imageBuffer, 0, 0); 205 | } 206 | 207 | private void panelImage_SizeChanged(object sender, EventArgs e) 208 | { 209 | RebuildBuffer(); 210 | RenderAxis(); 211 | panelImage.Refresh(); 212 | } 213 | 214 | private void MainForm_Shown(object sender, EventArgs e) 215 | { 216 | OpenClSetting oclSetting = new OpenClSetting(); 217 | oclSetting.ShowDialog(this); 218 | 219 | gpu = oclSetting.Gpu; 220 | } 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/MainForm.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/MainFormRenderer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using FvCalculation; 6 | using System.Drawing.Imaging; 7 | using System.Drawing; 8 | using System.Windows.Forms; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | 12 | namespace FvGUI 13 | { 14 | partial class MainForm 15 | { 16 | private void UpdateMessage(string message) 17 | { 18 | Invoke(new MethodInvoker(() => 19 | { 20 | labelErrorMessage.Text = message; 21 | })); 22 | } 23 | 24 | private void RenderAsnyc(Action renderer) 25 | { 26 | ThreadPool.QueueUserWorkItem(new WaitCallback(o => 27 | { 28 | Invoke(new MethodInvoker(() => 29 | { 30 | textBoxFunction.Enabled = false; 31 | textBoxUnitPixels.Enabled = false; 32 | textBoxOriginX.Enabled = false; 33 | textBoxOriginY.Enabled = false; 34 | buttonRender.Enabled = false; 35 | })); 36 | 37 | renderer(); 38 | 39 | Invoke(new MethodInvoker(() => 40 | { 41 | textBoxFunction.Enabled = true; 42 | textBoxUnitPixels.Enabled = true; 43 | textBoxOriginX.Enabled = true; 44 | textBoxOriginY.Enabled = true; 45 | buttonRender.Enabled = true; 46 | panelImage.Refresh(); 47 | })); 48 | }), null); 49 | } 50 | 51 | private void RenderFunction() 52 | { 53 | int w = this.imageBuffer.Width - 2; 54 | int h = this.imageBuffer.Height - 2; 55 | int cx = this.imageBuffer.Width / 2; 56 | int cy = this.imageBuffer.Height / 2; 57 | double[, ,] points = new double[h, w, 4]; 58 | 59 | RawExpression efx = this.function.Simplify(); 60 | var edfdx = efx.Different("x").Simplify(); 61 | var edfdy = efx.Different("y").Simplify(); 62 | 63 | var fx = efx.ToCode(); 64 | var dfdx = edfdx.ToCode(); 65 | var dfdy = edfdy.ToCode(); 66 | 67 | UpdateMessage("Computing..."); 68 | 69 | gpu.FillPoints(this.imageBuffer.Width, this.imageBuffer.Height, this.unitPixels, this.originX, this.originY, fx, dfdx, dfdy, points); 70 | 71 | //int done = 0; 72 | //int max = w + h; 73 | //int total = Environment.ProcessorCount * 8; 74 | //Parallel.For(0, total, (i) => 75 | //{ 76 | // int dh = (h + total - h % total) / total; 77 | // int dw = (w + total - w % total) / total; 78 | 79 | // int starty = 1 + i * dh; 80 | // int endy = Math.Min(h + 1, starty + dh); 81 | // int startx = 1 + i * dw; 82 | // int endx = Math.Min(w + 1, startx + dw); 83 | // int loops = (endy - starty + 1) + (endx - startx + 1); 84 | 85 | // for (int y = starty; y < endy; y++) 86 | // { 87 | // double py = this.originY + (double)(y - cy) / this.unitPixels; 88 | // RawExpression efx = this.function.Apply("y", py).Simplify(); 89 | // RawExpression edfx = efx.Different("x").Simplify(); 90 | // Func fx = efx.Compile("x"); 91 | // Func dfx = edfx.Compile("x"); 92 | 93 | // for (int x = 1; x <= w; x++) 94 | // { 95 | // points[y - 1, x - 1, 0] = fx.Solve(dfx, points[y - 1, x - 1, 0]); 96 | // } 97 | 98 | // int current = Interlocked.Increment(ref done); 99 | // if (current % 10 == 0) 100 | // { 101 | // UpdateMessage(current.ToString() + "/" + max.ToString()); 102 | // } 103 | // } 104 | 105 | // for (int x = startx; x < endx; x++) 106 | // { 107 | // double px = this.originX + (double)(cx - x) / this.unitPixels; 108 | // RawExpression efy = this.function.Apply("x", px).Simplify(); 109 | // RawExpression edfy = efy.Different("y").Simplify(); 110 | // Func fy = efy.Compile("y"); 111 | // Func dfy = edfy.Compile("y"); 112 | 113 | // for (int y = 1; y <= h; y++) 114 | // { 115 | // points[y - 1, x - 1, 3] = fy.Solve(dfy, points[y - 1, x - 1, 3]); 116 | // } 117 | 118 | // int current = Interlocked.Increment(ref done); 119 | // if (current % 10 == 0) 120 | // { 121 | // UpdateMessage(current.ToString() + "/" + max.ToString()); 122 | // } 123 | // } 124 | //}); 125 | 126 | UpdateMessage("Rendering..."); 127 | 128 | BitmapData data = this.imageBuffer.LockBits(new Rectangle(0, 0, this.imageBuffer.Width, this.imageBuffer.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 129 | for (int y = 1; y <= h; y++) 130 | { 131 | for (int x = 1; x <= w; x++) 132 | { 133 | double x1 = points[y - 1, x - 1, 0]; 134 | double y1 = points[y - 1, x - 1, 1]; 135 | double x2 = points[y - 1, x - 1, 2]; 136 | double y2 = points[y - 1, x - 1, 3]; 137 | 138 | if (!double.IsInfinity(x1) && !double.IsNaN(x1)) 139 | { 140 | Fill(data, x1, y1, cx, cy, w, h); 141 | } 142 | if (!double.IsInfinity(y2) && !double.IsNaN(y2)) 143 | { 144 | Fill(data, x2, y2, cx, cy, w, h); 145 | } 146 | } 147 | } 148 | this.imageBuffer.UnlockBits(data); 149 | 150 | UpdateMessage("(Ready)"); 151 | } 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/OpenClSetting.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace FvGUI 2 | { 3 | partial class OpenClSetting 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.label1 = new System.Windows.Forms.Label(); 32 | this.platformComboBox = new System.Windows.Forms.ComboBox(); 33 | this.label2 = new System.Windows.Forms.Label(); 34 | this.deviceComboBox = new System.Windows.Forms.ComboBox(); 35 | this.label3 = new System.Windows.Forms.Label(); 36 | this.label4 = new System.Windows.Forms.Label(); 37 | this.computeUnitLabel = new System.Windows.Forms.Label(); 38 | this.label5 = new System.Windows.Forms.Label(); 39 | this.vendorNameLabel = new System.Windows.Forms.Label(); 40 | this.fpTypeComboBox = new System.Windows.Forms.ComboBox(); 41 | this.exitButton = new System.Windows.Forms.Button(); 42 | this.okButton = new System.Windows.Forms.Button(); 43 | this.messageLabel = new System.Windows.Forms.Label(); 44 | this.label6 = new System.Windows.Forms.Label(); 45 | this.deviceTypeLabel = new System.Windows.Forms.Label(); 46 | this.SuspendLayout(); 47 | // 48 | // label1 49 | // 50 | this.label1.AutoSize = true; 51 | this.label1.Location = new System.Drawing.Point(10, 14); 52 | this.label1.Name = "label1"; 53 | this.label1.Size = new System.Drawing.Size(48, 13); 54 | this.label1.TabIndex = 0; 55 | this.label1.Text = "Platform:"; 56 | // 57 | // platformComboBox 58 | // 59 | this.platformComboBox.DisplayMember = "Name"; 60 | this.platformComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 61 | this.platformComboBox.FormattingEnabled = true; 62 | this.platformComboBox.Location = new System.Drawing.Point(13, 30); 63 | this.platformComboBox.Name = "platformComboBox"; 64 | this.platformComboBox.Size = new System.Drawing.Size(218, 21); 65 | this.platformComboBox.TabIndex = 1; 66 | this.platformComboBox.ValueMember = "Name"; 67 | this.platformComboBox.SelectedIndexChanged += new System.EventHandler(this.platformComboBox_SelectedIndexChanged); 68 | // 69 | // label2 70 | // 71 | this.label2.AutoSize = true; 72 | this.label2.Location = new System.Drawing.Point(10, 59); 73 | this.label2.Name = "label2"; 74 | this.label2.Size = new System.Drawing.Size(44, 13); 75 | this.label2.TabIndex = 2; 76 | this.label2.Text = "Device:"; 77 | // 78 | // deviceComboBox 79 | // 80 | this.deviceComboBox.DisplayMember = "Name"; 81 | this.deviceComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 82 | this.deviceComboBox.FormattingEnabled = true; 83 | this.deviceComboBox.Location = new System.Drawing.Point(13, 75); 84 | this.deviceComboBox.Name = "deviceComboBox"; 85 | this.deviceComboBox.Size = new System.Drawing.Size(218, 21); 86 | this.deviceComboBox.TabIndex = 3; 87 | this.deviceComboBox.ValueMember = "Name"; 88 | this.deviceComboBox.SelectedIndexChanged += new System.EventHandler(this.deviceComboBox_SelectedIndexChanged); 89 | // 90 | // label3 91 | // 92 | this.label3.AutoSize = true; 93 | this.label3.Location = new System.Drawing.Point(10, 104); 94 | this.label3.Name = "label3"; 95 | this.label3.Size = new System.Drawing.Size(106, 13); 96 | this.label3.TabIndex = 4; 97 | this.label3.Text = "Float Point Precision:"; 98 | // 99 | // label4 100 | // 101 | this.label4.AutoSize = true; 102 | this.label4.Location = new System.Drawing.Point(237, 76); 103 | this.label4.Name = "label4"; 104 | this.label4.Size = new System.Drawing.Size(74, 13); 105 | this.label4.TabIndex = 5; 106 | this.label4.Text = "Compute Unit:"; 107 | // 108 | // computeUnitLabel 109 | // 110 | this.computeUnitLabel.AutoSize = true; 111 | this.computeUnitLabel.Location = new System.Drawing.Point(317, 76); 112 | this.computeUnitLabel.Name = "computeUnitLabel"; 113 | this.computeUnitLabel.Size = new System.Drawing.Size(57, 13); 114 | this.computeUnitLabel.TabIndex = 6; 115 | this.computeUnitLabel.Text = "(unknown)"; 116 | // 117 | // label5 118 | // 119 | this.label5.AutoSize = true; 120 | this.label5.Location = new System.Drawing.Point(237, 33); 121 | this.label5.Name = "label5"; 122 | this.label5.Size = new System.Drawing.Size(44, 13); 123 | this.label5.TabIndex = 7; 124 | this.label5.Text = "Vendor:"; 125 | // 126 | // vendorNameLabel 127 | // 128 | this.vendorNameLabel.Location = new System.Drawing.Point(287, 33); 129 | this.vendorNameLabel.Name = "vendorNameLabel"; 130 | this.vendorNameLabel.Size = new System.Drawing.Size(120, 39); 131 | this.vendorNameLabel.TabIndex = 8; 132 | this.vendorNameLabel.Text = "(unknown)"; 133 | // 134 | // fpTypeComboBox 135 | // 136 | this.fpTypeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 137 | this.fpTypeComboBox.FormattingEnabled = true; 138 | this.fpTypeComboBox.Items.AddRange(new object[] { 139 | "Single Precision"}); 140 | this.fpTypeComboBox.Location = new System.Drawing.Point(13, 120); 141 | this.fpTypeComboBox.Name = "fpTypeComboBox"; 142 | this.fpTypeComboBox.Size = new System.Drawing.Size(218, 21); 143 | this.fpTypeComboBox.TabIndex = 9; 144 | this.fpTypeComboBox.SelectedIndexChanged += new System.EventHandler(this.fpTypeComboBox_SelectedIndexChanged); 145 | // 146 | // exitButton 147 | // 148 | this.exitButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; 149 | this.exitButton.Location = new System.Drawing.Point(332, 205); 150 | this.exitButton.Name = "exitButton"; 151 | this.exitButton.Size = new System.Drawing.Size(75, 23); 152 | this.exitButton.TabIndex = 10; 153 | this.exitButton.Text = "E&xit"; 154 | this.exitButton.UseVisualStyleBackColor = true; 155 | this.exitButton.Click += new System.EventHandler(this.exitButton_Click); 156 | // 157 | // okButton 158 | // 159 | this.okButton.Enabled = false; 160 | this.okButton.Location = new System.Drawing.Point(251, 205); 161 | this.okButton.Name = "okButton"; 162 | this.okButton.Size = new System.Drawing.Size(75, 23); 163 | this.okButton.TabIndex = 11; 164 | this.okButton.Text = "OK"; 165 | this.okButton.UseVisualStyleBackColor = true; 166 | this.okButton.Click += new System.EventHandler(this.okButton_Click); 167 | // 168 | // messageLabel 169 | // 170 | this.messageLabel.Location = new System.Drawing.Point(10, 148); 171 | this.messageLabel.Name = "messageLabel"; 172 | this.messageLabel.Size = new System.Drawing.Size(397, 54); 173 | this.messageLabel.TabIndex = 12; 174 | this.messageLabel.Text = "Ready"; 175 | // 176 | // label6 177 | // 178 | this.label6.AutoSize = true; 179 | this.label6.Location = new System.Drawing.Point(237, 104); 180 | this.label6.Name = "label6"; 181 | this.label6.Size = new System.Drawing.Size(71, 13); 182 | this.label6.TabIndex = 13; 183 | this.label6.Text = "Device Type:"; 184 | // 185 | // deviceTypeLabel 186 | // 187 | this.deviceTypeLabel.AutoSize = true; 188 | this.deviceTypeLabel.Location = new System.Drawing.Point(314, 104); 189 | this.deviceTypeLabel.Name = "deviceTypeLabel"; 190 | this.deviceTypeLabel.Size = new System.Drawing.Size(57, 13); 191 | this.deviceTypeLabel.TabIndex = 14; 192 | this.deviceTypeLabel.Text = "(unknown)"; 193 | // 194 | // OpenClSetting 195 | // 196 | this.AcceptButton = this.okButton; 197 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 198 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 199 | this.CancelButton = this.exitButton; 200 | this.ClientSize = new System.Drawing.Size(419, 240); 201 | this.Controls.Add(this.deviceTypeLabel); 202 | this.Controls.Add(this.label6); 203 | this.Controls.Add(this.messageLabel); 204 | this.Controls.Add(this.okButton); 205 | this.Controls.Add(this.exitButton); 206 | this.Controls.Add(this.fpTypeComboBox); 207 | this.Controls.Add(this.vendorNameLabel); 208 | this.Controls.Add(this.label5); 209 | this.Controls.Add(this.computeUnitLabel); 210 | this.Controls.Add(this.label4); 211 | this.Controls.Add(this.label3); 212 | this.Controls.Add(this.deviceComboBox); 213 | this.Controls.Add(this.label2); 214 | this.Controls.Add(this.platformComboBox); 215 | this.Controls.Add(this.label1); 216 | this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 217 | this.MaximizeBox = false; 218 | this.MinimizeBox = false; 219 | this.Name = "OpenClSetting"; 220 | this.ShowInTaskbar = false; 221 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; 222 | this.Text = "OpenCL Settings"; 223 | this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.OpenClSetting_FormClosed); 224 | this.Load += new System.EventHandler(this.OpenClSetting_Load); 225 | this.ResumeLayout(false); 226 | this.PerformLayout(); 227 | 228 | } 229 | 230 | #endregion 231 | 232 | private System.Windows.Forms.Label label1; 233 | private System.Windows.Forms.ComboBox platformComboBox; 234 | private System.Windows.Forms.Label label2; 235 | private System.Windows.Forms.ComboBox deviceComboBox; 236 | private System.Windows.Forms.Label label3; 237 | private System.Windows.Forms.Label label4; 238 | private System.Windows.Forms.Label computeUnitLabel; 239 | private System.Windows.Forms.Label label5; 240 | private System.Windows.Forms.Label vendorNameLabel; 241 | private System.Windows.Forms.ComboBox fpTypeComboBox; 242 | private System.Windows.Forms.Button exitButton; 243 | private System.Windows.Forms.Button okButton; 244 | private System.Windows.Forms.Label messageLabel; 245 | private System.Windows.Forms.Label label6; 246 | private System.Windows.Forms.Label deviceTypeLabel; 247 | } 248 | } -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/OpenClSetting.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Windows.Forms; 9 | using Cloo; 10 | 11 | namespace FvGUI 12 | { 13 | public partial class OpenClSetting : Form 14 | { 15 | public OpenClSetting() 16 | { 17 | InitializeComponent(); 18 | } 19 | 20 | private void OpenClSetting_Load(object sender, EventArgs e) 21 | { 22 | Gpu = null; 23 | 24 | var platforms = ComputePlatform.Platforms; 25 | 26 | if (platforms.Count == 0) 27 | { 28 | platformComboBox.Enabled = false; 29 | deviceComboBox.Enabled = false; 30 | fpTypeComboBox.Enabled = false; 31 | okButton.Enabled = false; 32 | 33 | messageLabel.Text = "There's no OpenCL platform installed on this computer"; 34 | 35 | return; 36 | } 37 | 38 | platformComboBox.Items.AddRange(platforms.ToArray()); 39 | platformComboBox.SelectedIndex = 0; 40 | } 41 | 42 | private void OpenClSetting_FormClosed(object sender, FormClosedEventArgs e) 43 | { 44 | if (Gpu == null) 45 | { 46 | Application.Exit(); 47 | } 48 | 49 | } 50 | 51 | private void exitButton_Click(object sender, EventArgs e) 52 | { 53 | Close(); 54 | } 55 | 56 | private void platformComboBox_SelectedIndexChanged(object sender, EventArgs e) 57 | { 58 | if (platformComboBox.SelectedIndex < 0) 59 | { 60 | deviceComboBox.Enabled = false; 61 | deviceComboBox.Items.Clear(); 62 | 63 | fpTypeComboBox.Enabled = false; 64 | fpTypeComboBox.Items.Clear(); 65 | 66 | okButton.Enabled = false; 67 | vendorNameLabel.Text = "(unknown)"; 68 | computeUnitLabel.Text = "(unknown)"; 69 | deviceTypeLabel.Text = "(unknown)"; 70 | 71 | return; 72 | } 73 | 74 | var platform = platformComboBox.SelectedItem as ComputePlatform; 75 | 76 | vendorNameLabel.Text = platform.Vendor; 77 | 78 | var devices = platform.Devices; 79 | 80 | if (devices.Count == 0) 81 | { 82 | deviceComboBox.Enabled = false; 83 | deviceComboBox.Items.Clear(); 84 | 85 | fpTypeComboBox.Enabled = false; 86 | fpTypeComboBox.Items.Clear(); 87 | 88 | okButton.Enabled = false; 89 | vendorNameLabel.Text = "(unknown)"; 90 | computeUnitLabel.Text = "(unknown)"; 91 | deviceTypeLabel.Text = "(unknown)"; 92 | 93 | messageLabel.Text = "There's no compute device available on this platform."; 94 | 95 | return; 96 | } 97 | 98 | deviceComboBox.Items.Clear(); 99 | deviceComboBox.Items.AddRange(devices.ToArray()); 100 | deviceComboBox.SelectedIndex = 0; 101 | } 102 | 103 | private void deviceComboBox_SelectedIndexChanged(object sender, EventArgs e) 104 | { 105 | if (deviceComboBox.SelectedIndex < 0) 106 | { 107 | fpTypeComboBox.Enabled = false; 108 | fpTypeComboBox.Items.Clear(); 109 | 110 | okButton.Enabled = false; 111 | computeUnitLabel.Text = "(unknown)"; 112 | deviceTypeLabel.Text = "(unknown)"; 113 | 114 | return; 115 | } 116 | 117 | var device = deviceComboBox.SelectedItem as ComputeDevice; 118 | 119 | computeUnitLabel.Text = device.MaxComputeUnits.ToString(); 120 | deviceTypeLabel.Text = device.Type.ToString(); 121 | 122 | //query float point capability 123 | 124 | fpTypeComboBox.Items.Clear(); 125 | 126 | fpTypeComboBox.Items.Add("Single Precision"); 127 | 128 | fpTypeComboBox.SelectedIndex = 0; 129 | 130 | if (device.Extensions.Contains("cl_amd_fp64")) 131 | { 132 | fpTypeComboBox.Items.Add("Double Precision (AMD)"); 133 | fpTypeComboBox.SelectedItem = "Double Precision (AMD)"; 134 | } 135 | 136 | if (device.Extensions.Contains("cl_khr_fp64")) 137 | { 138 | fpTypeComboBox.Items.Add("Double Precision"); 139 | fpTypeComboBox.SelectedItem = "Double Precision"; 140 | } 141 | } 142 | 143 | private void fpTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) 144 | { 145 | if (fpTypeComboBox.SelectedIndex < 0) 146 | { 147 | okButton.Enabled = false; 148 | return; 149 | } 150 | 151 | messageLabel.Text = "Ready"; 152 | 153 | okButton.Enabled = true; 154 | } 155 | 156 | internal IGpuHelper Gpu { get; private set; } 157 | 158 | private void okButton_Click(object sender, EventArgs e) 159 | { 160 | FPType fpType; 161 | 162 | if (fpTypeComboBox.SelectedItem as string == "Double Precision (AMD)") 163 | { 164 | fpType = FPType.FP64AMD; 165 | } 166 | else if (fpTypeComboBox.SelectedItem as string == "Double Precision") 167 | { 168 | fpType = FPType.FP64; 169 | } 170 | else 171 | { 172 | fpType = FPType.Single; 173 | } 174 | 175 | Gpu = GpuHelperFactory.CreateHelper(platformComboBox.SelectedItem as ComputePlatform, deviceComboBox.SelectedItem as ComputeDevice, fpType); 176 | 177 | Close(); 178 | } 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/OpenClSetting.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Windows.Forms; 5 | 6 | namespace FvGUI 7 | { 8 | static class Program 9 | { 10 | /// 11 | /// The main entry point for the application. 12 | /// 13 | [STAThread] 14 | static void Main() 15 | { 16 | Application.EnableVisualStyles(); 17 | Application.SetCompatibleTextRenderingDefault(false); 18 | Application.Run(new MainForm()); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("FvGUI")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Microsoft")] 12 | [assembly: AssemblyProduct("FvGUI")] 13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("c4fff142-4527-4fe1-916f-c983eb1fc1c6")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.235 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace FvGUI.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// Returns the cached ResourceManager instance used by this class. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("FvGUI.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// Overrides the current thread's CurrentUICulture property for all 51 | /// resource lookups using this strongly typed resource class. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | 63 | internal static byte[] function_vis { 64 | get { 65 | object obj = ResourceManager.GetObject("function_vis", resourceCulture); 66 | return ((byte[])(obj)); 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 122 | ..\function_vis.cl;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 123 | 124 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.235 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace FvGUI.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /FunctionVisualizer/FvGUI/function_vis.cl: -------------------------------------------------------------------------------- 1 | #ifdef FP64 2 | 3 | #pragma OPENCL EXTENSION cl_khr_fp64 : enable 4 | typedef double fp_t; 5 | #define _F(x) x 6 | #define EPSILON 0.000001 7 | 8 | #elif defined AMDFP64 9 | 10 | #pragma OPENCL EXTENSION cl_amd_fp64 : enable 11 | typedef double fp_t; 12 | #define _F(x) x 13 | #define EPSILON 0.000001 14 | 15 | #else 16 | 17 | typedef float fp_t; 18 | #define _F(x) x##f 19 | #define EPSILON 0.00001f 20 | 21 | #endif 22 | 23 | #define MAX_ITER 256 24 | 25 | fp_t func(fp_t x, fp_t y) 26 | { 27 | return {func}; 28 | } 29 | 30 | fp_t df_dx(fp_t x, fp_t y) 31 | { 32 | return {dfuncdx}; 33 | } 34 | 35 | fp_t df_dy(fp_t x, fp_t y) 36 | { 37 | return {dfuncdy}; 38 | } 39 | 40 | fp_t solvex(fp_t start, const fp_t consty) 41 | { 42 | for (int i = 0; i < MAX_ITER; ++i) 43 | { 44 | fp_t result = func(start, consty); 45 | 46 | if (result <= EPSILON && result >= -EPSILON) 47 | { 48 | return start; 49 | } 50 | 51 | fp_t d = df_dx(start, consty); 52 | if (d <= EPSILON && d >= -EPSILON) 53 | { 54 | return NAN; 55 | } 56 | else 57 | { 58 | start -= result / d; 59 | } 60 | } 61 | return NAN; 62 | } 63 | 64 | fp_t solvey(fp_t start, const fp_t constx) 65 | { 66 | for (int i = 0; i < MAX_ITER; ++i) 67 | { 68 | fp_t result = func(constx, start); 69 | 70 | if (result <= EPSILON && result >= -EPSILON) 71 | { 72 | return start; 73 | } 74 | 75 | fp_t d = df_dy(constx, start); 76 | if (d <= EPSILON && d >= -EPSILON) 77 | { 78 | return NAN; 79 | } 80 | else 81 | { 82 | start -= result / d; 83 | } 84 | } 85 | return NAN; 86 | } 87 | 88 | kernel void ComputeX( 89 | global write_only fp_t* points, 90 | int unit, 91 | int width, 92 | int cx, 93 | int cy, 94 | float origin_x, 95 | float origin_y) 96 | { 97 | int gx = get_global_id(0); 98 | int gy = get_global_id(1); 99 | 100 | uint write_loc = gx + gy * width; 101 | 102 | fp_t py = origin_y + (fp_t)(gy + 1 - cy) / unit; 103 | fp_t px = origin_x + (fp_t)(cx - gx - 1) / unit; 104 | 105 | points[write_loc] = solvex(px, py); 106 | } 107 | 108 | kernel void ComputeY( 109 | global write_only fp_t* points, 110 | int unit, 111 | int width, 112 | int cx, 113 | int cy, 114 | float origin_x, 115 | float origin_y) 116 | { 117 | int gx = get_global_id(0); 118 | int gy = get_global_id(1); 119 | 120 | uint write_loc = gx + gy * width; 121 | 122 | fp_t py = origin_y + (fp_t)(gy + 1 - cy) / unit; 123 | fp_t px = origin_x + (fp_t)(cx - gx - 1) / unit; 124 | 125 | points[write_loc] = solvey(py, px); 126 | } --------------------------------------------------------------------------------