///
/// This file is part of ILNumerics Community Edition.
///
/// ILNumerics Community Edition - high performance computing for applications.
/// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
///
/// ILNumerics Community Edition is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License version 3 as published by
/// the Free Software Foundation.
///
/// ILNumerics Community Edition is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with ILNumerics Community Edition. See the file License.txt in the root
/// of your distribution package. If not, see .
///
/// In addition this software uses the following components and/or licenses:
///
/// =================================================================================
/// The Open Toolkit Library License
///
/// Copyright (c) 2006 - 2009 the Open Toolkit library.
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights to
/// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
/// the Software, and to permit persons to whom the Software is furnished to do
/// so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all
/// copies or substantial portions of the Software.
///
/// =================================================================================
///
#pragma warning disable 1591
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using ILNumerics.Storage;
namespace ILNumerics.Misc {
public class ILExpression : ILDenseArray {
public ILExpression(Expression exp) : base(new ILDenseStorage(
new Expression[1] { exp }, ILSize.Scalar1_1), false) {
}
public Expression Expression {
get { return GetValue(0); }
}
#region operator overload
public static ILExpression operator -(ILExpression a, ILExpression b) {
return new ILExpression(Expression.Subtract(a.Expression, b.Expression));
}
public static ILExpression operator +(ILExpression a, ILExpression b) {
return new ILExpression(Expression.Add(a.Expression, b.Expression));
}
public static ILExpression operator *(ILExpression a, ILExpression b) {
return new ILExpression(Expression.Multiply(a.Expression, b.Expression));
}
public static ILExpression operator /(ILExpression a, ILExpression b) {
return new ILExpression(Expression.Divide(a.Expression, b.Expression));
}
public static ILExpression operator +(ILExpression expr, ILBaseArray a) {
return new ILExpression(Expression.Add(expr.Expression, BA2Expr(a)));
}
public static ILExpression operator +(ILBaseArray a, ILExpression expr) {
return new ILExpression(Expression.Add(BA2Expr(a), expr.Expression));
}
public static ILExpression operator -(ILExpression expr, ILBaseArray a) {
return new ILExpression(Expression.Subtract(expr.Expression, BA2Expr(a)));
}
public static ILExpression operator -(ILBaseArray a, ILExpression expr) {
return new ILExpression(Expression.Subtract(BA2Expr(a), expr.Expression));
}
public static ILExpression operator /(ILExpression expr, ILBaseArray a) {
return new ILExpression(Expression.Divide(expr.Expression, BA2Expr(a)));
}
public static ILExpression operator /(ILBaseArray a, ILExpression expr) {
return new ILExpression(Expression.Divide(BA2Expr(a), expr.Expression));
}
public static ILExpression operator *(ILExpression expr, ILBaseArray a) {
return new ILExpression(Expression.Multiply(expr.Expression, BA2Expr(a)));
}
public static ILExpression operator *(ILBaseArray a, ILExpression expr) {
return new ILExpression(Expression.Multiply(BA2Expr(a), expr.Expression));
}
#endregion
#region helper
private static MethodCallExpression BA2Expr(ILBaseArray a) {
MemberExpression storageExpr = Expression.Property(Expression.Constant(a), typeof(ILBaseArray).GetProperty("Storage", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance));
MethodCallExpression getValExpr = Expression.Call(
storageExpr
, typeof(ILStorage).GetMethod("GetValue", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
, Expression.Constant(new int[] { 0 }));
MethodCallExpression convertInt32 = Expression.Call(
typeof(Convert).GetMethod("ToInt32", new Type[] { typeof(object) })
, getValExpr);
return convertInt32;
}
#endregion
#region ILBaseArray Interface
public override bool IsComplex {
get { return false; }
}
public override bool IsNumeric {
get { return false; }
}
public override void ToStream(System.IO.Stream outStream, string format, ILArrayStreamSerializationFlags method) {
throw new NotImplementedException();
}
internal override bool EnterScope() {
return false;
}
#endregion ILBaseArray Interface
internal static int Evaluate(Expression iLExpression, int dimLength) {
ParameterExpression endParameter = (ParameterExpression)ILMath.end.Expression;
int res = Expression.Lambda>(iLExpression,new ParameterExpression[]{ endParameter }).Compile()(dimLength);
return res;
}
}
}