Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6188771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:17:36+00:00 2026-05-24T02:17:36+00:00

I must admit that I’m absolutely new to Expression Trees in C#, but currently

  • 0

I must admit that I’m absolutely new to Expression Trees in C#, but currently there is a necessity to get used to it. My problem is that I have two DataTypes that contain an array of arrays. For that reason I created an interface which is called IArray<>

    /// <summary>
    /// Interface for all classes that provide a list of IArrayData
    /// </summary>
    public interface IArray<ElementDataType>
        where ElementDataType : struct
    {
        /// <summary>
        /// Returns a single data out fo the array container.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        IArrayData<ElementDataType> GetElementData(int index);

        /// <summary>
        /// Returns the amount of ArrayData.
        /// </summary>
        int Count
        {
            get;
        }

        /// <summary>
        /// Returns the size of a single dataset in number of elements
        /// (not in bytes!).
        /// </summary>
        /// <returns></returns>
        int GetDataSize();

        /// <summary>
        /// Creates a copy of the array data provider, by copying the metainformation, but not the
        /// contents.
        /// </summary>
        /// <returns></returns>
        IArray<ElementDataType> CloneStub();
    }

IArrayData is defined as following:

   /// <summary>
    /// DataProvider that provides the internal data as an array.
    /// </summary>
    /// <typeparam name="NativeDataType"></typeparam>
    public interface IArrayData<NativeDataType> where NativeDataType : struct
    {
        /// <summary>
        /// Returns the data in an arbitrary format.
        /// The implementor must take care of an appropriate cast.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        T[] GetData<T>() where T : struct;

        /// <summary>
        /// Returns the data as float[].
        /// </summary>
        /// <returns></returns>
        float[] GetData();

        /// <summary>
        /// Sets the data in an arbitrary format.
        /// The implementor must take care of an appropriate cast.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data_in"></param>
        void SetData<T>(T[] data_in) where T : struct;

        /// <summary>
        /// Sets the data as float[].
        /// </summary>
        /// <param name="data_in"></param>
        void SetData(float[] data_in);
    }

There are two types that implement the interface. On most of the data I have to perform mathematical operations on the data. Currently I have created my own expression evaluator, but I would love to use the expression trees because it seems to me that it’s more flexible. How would I implement a mathematical operation like +, – on the given interface?

One type I have is what I call VoxelVolume, which is to store a 3d block of image data. VoxelVolume implements IArray:

public abstract class VoxelVolume : IArray<float>
{
}

Lets assume I have 3 VoxelVolumes A, B, and C. Now I want to perform an operation:

VoxelVolume D = (A + B) * C;

Currently I’m doing this with operator overloading and it works quite well. The only problem is, that the expression is evaluated operation by operation and the longer the expression is the more time and memory it takes. I would prefer to combine the operation in one single step. This is what my current implementation does

public static IArray<float> AddMul(IArray<float> A, IArray<float> B, IArray<float> C)
{
    IArray<float> Result = A.CloneStub();

    int L = A.Count;
    int N = A.GetDataSize();

    for (int l = 0; l < L; l++)
    {
        float[] a = A.GetElementData(l).GetData();
        float[] b = B.GetElementData(l).GetData();
        float[] c = C.GetElementData(l).GetData();
        float[] d = new float[a.Length];

        for (int n = 0; n < N; n++)
        {
            d[n] = (a[n] + b[n]) * c[n];
        }

        Result.GetElementData(l).SetData(d);
    }

    return Result;
}

But as you may recognize I have to type a lot for all different operations +,-,*,/ and a lot more. For that reasons I’d like to have a more generic and flexible way to perform this operations.

Thanks
Martin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:17:37+00:00Added an answer on May 24, 2026 at 2:17 am

    In your example, I’m assuming that it would be reasonable to run the GetData() calls in your standard code, although since we might not know the “depth”, we can simplify to a jagged array (a rectangular array would work too, but is harder to work with at all points, so let’s not do that). So imagine we have a jagged array instead of a,b,c (although we’ll assume the answers d is simple enough). Thus, we actually need to build:

    = ((jagged[0])[n] + (jagged[1])[n]) * (jagged[2])[n]
    

    which is, as a tree:

    = *( +((jagged[0])[n], (jagged[1])[n]), (jagged[2])[n])
    

    So we can build an expression (and evaluate), via:

    var data = Expression.Parameter(typeof (float[][]));
    var n = Expression.Parameter(typeof (int));
    
    var body = 
        Expression.Multiply( // *
            Expression.Add( // +
                Expression.ArrayIndex(
                    Expression.ArrayIndex(data, Expression.Constant(0)), n), // a[n]
                Expression.ArrayIndex(
                    Expression.ArrayIndex(data, Expression.Constant(1)), n) // b[n]
                ),
            Expression.ArrayIndex(
                Expression.ArrayIndex(data, Expression.Constant(2)), n)  // c[n]
        );
    var func = Expression.Lambda<Func<float[][], int, float>>(body, data, n).Compile();
    
    
    // here, a===jagged[0], b===jagged[1], c===jagged[2]
    float[][] jagged = new[] { new[] { 1F, 2F }, new[] { 3F, 4F }, new[] { 5F, 6F } };
    for(int i = 0; i < 2; i++)
    {
        Console.WriteLine("{0}: {1}", i, func(jagged, i));
    }
    

    Obviously your actual code needs to parse your input expression and build a similar tree flexibly; but this should be enough to illustrate a general approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I must first admit that I'm new to Analysis Services but now must extend
I must admit that I am new to jQuery and JS but I really
I'm new to jQuery and i must admit that i've understood nothing yet, the
I must admit that I am incredibly jealous of those developers who happen to
I must admit that I don't really know if this is the right place
I have just installed Microsoft Robotics Studio 2008 R2, and I must admit that
I have this simple script that I'm working on. I must admit, I'm totally
This problem is about concurrent high speed inserts. I must admit it is very
I recently used Meebo and I must admit I'm a little paranoid about typing
I must admit I have researched a bit but keep coming into a block

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.