Advertisement
Guest User

Untitled

a guest
May 24th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6.  
  7. namespace ConsoleApplication12
  8. {
  9. class Program
  10. {
  11.  
  12. static void Main(string[] args)
  13. {
  14.  
  15. ParameterExpression value = Expression.Parameter(typeof(double), "value");
  16. ParameterExpression value2 = Expression.Parameter(typeof(double), "value2");
  17.  
  18. // Creating an expression to hold a local variable.
  19. ParameterExpression result = Expression.Parameter(typeof(double), "result");
  20.  
  21.  
  22. // Creating a label to jump to from a loop.
  23. LabelTarget label = Expression.Label(typeof(double));
  24.  
  25. // Creating a method body.
  26. BlockExpression block = Expression.Block(
  27. // Adding a local variable.
  28. new[] { result },
  29. // Assigning a constant to a local variable: result = 1
  30. Expression.Assign(result, Expression.Constant(1.0)),
  31.  
  32. // Adding a loop.
  33. Expression.Loop(
  34. // Adding a conditional block into the loop.
  35. Expression.IfThenElse(
  36. // Condition: value > 1
  37. Expression.GreaterThan(value2, Expression.Constant(0.0)),
  38. // If true: result *= value --
  39. Expression.MultiplyAssign(result,
  40. Expression.Divide(value,Expression.PostDecrementAssign(value2))),
  41. // If false, exit the loop and go to the label.
  42. Expression.Break(label, result)
  43. ),
  44. // Label to jump to.
  45. label
  46. )
  47. );
  48.  
  49. // Compile and execute an expression tree.
  50. for (double j = 0.1; j <= 1.01; j += 0.05)
  51. {
  52. int i = 0;
  53. double sum = 1, res = 0;
  54. while (sum > 0.0004)
  55. {
  56. sum = Expression.Lambda<Func<double, double, double>>(block, value, value2).Compile()(j, 2 * i + 1);
  57. i++;
  58. res += sum;
  59. }
  60. double y = 0;
  61. y = (Math.Pow(Math.E, j) - Math.Pow(Math.E, -j)) / 2;
  62. Console.WriteLine("{0:f2} {1:f4} {2:f4}", j, res, y);
  63. }
  64. Console.ReadKey();
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement