1 | ///
|
---|
2 | /// This file is part of ILNumerics Community Edition.
|
---|
3 | ///
|
---|
4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
6 | ///
|
---|
7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
9 | /// the Free Software Foundation.
|
---|
10 | ///
|
---|
11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | /// GNU General Public License for more details.
|
---|
15 | ///
|
---|
16 | /// You should have received a copy of the GNU General Public License
|
---|
17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ///
|
---|
20 | /// In addition this software uses the following components and/or licenses:
|
---|
21 | ///
|
---|
22 | /// =================================================================================
|
---|
23 | /// The Open Toolkit Library License
|
---|
24 | ///
|
---|
25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
26 | ///
|
---|
27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
29 | /// in the Software without restriction, including without limitation the rights to
|
---|
30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
32 | /// so, subject to the following conditions:
|
---|
33 | ///
|
---|
34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
35 | /// copies or substantial portions of the Software.
|
---|
36 | ///
|
---|
37 | /// =================================================================================
|
---|
38 | ///
|
---|
39 |
|
---|
40 | #pragma warning disable 1591
|
---|
41 |
|
---|
42 |
|
---|
43 | using System;
|
---|
44 | using System.Collections.Generic;
|
---|
45 | using System.Linq;
|
---|
46 | using System.Text;
|
---|
47 | using System.Linq.Expressions;
|
---|
48 | using ILNumerics.Storage;
|
---|
49 |
|
---|
50 | namespace ILNumerics.Misc {
|
---|
51 | public class ILExpression : ILDenseArray<Expression> {
|
---|
52 |
|
---|
53 | public ILExpression(Expression exp) : base(new ILDenseStorage<Expression>(
|
---|
54 | new Expression[1] { exp }, ILSize.Scalar1_1), false) {
|
---|
55 | }
|
---|
56 |
|
---|
57 | public Expression Expression {
|
---|
58 | get { return GetValue(0); }
|
---|
59 | }
|
---|
60 |
|
---|
61 | #region operator overload
|
---|
62 | public static ILExpression operator -(ILExpression a, ILExpression b) {
|
---|
63 | return new ILExpression(Expression.Subtract(a.Expression, b.Expression));
|
---|
64 | }
|
---|
65 | public static ILExpression operator +(ILExpression a, ILExpression b) {
|
---|
66 | return new ILExpression(Expression.Add(a.Expression, b.Expression));
|
---|
67 | }
|
---|
68 | public static ILExpression operator *(ILExpression a, ILExpression b) {
|
---|
69 | return new ILExpression(Expression.Multiply(a.Expression, b.Expression));
|
---|
70 | }
|
---|
71 | public static ILExpression operator /(ILExpression a, ILExpression b) {
|
---|
72 | return new ILExpression(Expression.Divide(a.Expression, b.Expression));
|
---|
73 | }
|
---|
74 |
|
---|
75 | public static ILExpression operator +(ILExpression expr, ILBaseArray a) {
|
---|
76 | return new ILExpression(Expression.Add(expr.Expression, BA2Expr(a)));
|
---|
77 | }
|
---|
78 | public static ILExpression operator +(ILBaseArray a, ILExpression expr) {
|
---|
79 | return new ILExpression(Expression.Add(BA2Expr(a), expr.Expression));
|
---|
80 | }
|
---|
81 | public static ILExpression operator -(ILExpression expr, ILBaseArray a) {
|
---|
82 | return new ILExpression(Expression.Subtract(expr.Expression, BA2Expr(a)));
|
---|
83 | }
|
---|
84 | public static ILExpression operator -(ILBaseArray a, ILExpression expr) {
|
---|
85 | return new ILExpression(Expression.Subtract(BA2Expr(a), expr.Expression));
|
---|
86 | }
|
---|
87 |
|
---|
88 | public static ILExpression operator /(ILExpression expr, ILBaseArray a) {
|
---|
89 | return new ILExpression(Expression.Divide(expr.Expression, BA2Expr(a)));
|
---|
90 | }
|
---|
91 | public static ILExpression operator /(ILBaseArray a, ILExpression expr) {
|
---|
92 | return new ILExpression(Expression.Divide(BA2Expr(a), expr.Expression));
|
---|
93 | }
|
---|
94 | public static ILExpression operator *(ILExpression expr, ILBaseArray a) {
|
---|
95 | return new ILExpression(Expression.Multiply(expr.Expression, BA2Expr(a)));
|
---|
96 | }
|
---|
97 | public static ILExpression operator *(ILBaseArray a, ILExpression expr) {
|
---|
98 | return new ILExpression(Expression.Multiply(BA2Expr(a), expr.Expression));
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | #region helper
|
---|
103 | private static MethodCallExpression BA2Expr(ILBaseArray a) {
|
---|
104 | MemberExpression storageExpr = Expression.Property(Expression.Constant(a), typeof(ILBaseArray).GetProperty("Storage", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance));
|
---|
105 | MethodCallExpression getValExpr = Expression.Call(
|
---|
106 | storageExpr
|
---|
107 | , typeof(ILStorage).GetMethod("GetValue", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
|
---|
108 | , Expression.Constant(new int[] { 0 }));
|
---|
109 | MethodCallExpression convertInt32 = Expression.Call(
|
---|
110 | typeof(Convert).GetMethod("ToInt32", new Type[] { typeof(object) })
|
---|
111 | , getValExpr);
|
---|
112 | return convertInt32;
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | #region ILBaseArray Interface
|
---|
117 |
|
---|
118 | public override bool IsComplex {
|
---|
119 | get { return false; }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override bool IsNumeric {
|
---|
123 | get { return false; }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public override void ToStream(System.IO.Stream outStream, string format, ILArrayStreamSerializationFlags method) {
|
---|
127 | throw new NotImplementedException();
|
---|
128 | }
|
---|
129 | internal override bool EnterScope() {
|
---|
130 | return false;
|
---|
131 | }
|
---|
132 | #endregion ILBaseArray Interface
|
---|
133 |
|
---|
134 | internal static int Evaluate(Expression iLExpression, int dimLength) {
|
---|
135 | ParameterExpression endParameter = (ParameterExpression)ILMath.end.Expression;
|
---|
136 | int res = Expression.Lambda<Func<int,int>>(iLExpression,new ParameterExpression[]{ endParameter }).Compile()(dimLength);
|
---|
137 | return res;
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|
141 | }
|
---|