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

  • SEARCH
  • Home
  • 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 425017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:20:15+00:00 2026-05-12T19:20:15+00:00

I spend much of my time programming in R or MATLAB. These languages are

  • 0

I spend much of my time programming in R or MATLAB. These languages are typically used for manipulating arrays and matrices, and consequently, they have vectorised operators for addition, equality, etc.

For example, in MATLAB, adding two arrays

[1.2 3.4 5.6] + [9.87 6.54 3.21]

returns an array of the same size

ans =
        11.07         9.94         8.81

Switching over to C#, we need a loop, and it feels like a lot of code.

double[] a = { 1.2, 3.4, 5.6 };
double[] b = { 9.87, 6.54, 3.21 };
double[] sum = new double[a.Length];
for (int i = 0; i < a.Length; ++i)
{
  sum[i] = a[i] + b[i];
}

How should I implement vectorised operators using C#? These should preferably work for all numeric array types (and bool[]). Working for multidimensional arrays is a bonus.

The first idea I had was to overload the operators for System.Double[], etc. directly. This has a number of problems though. Firstly, it could cause confusion and maintainability issues if built-in classes do not bahave as expected. Secondly, I’m not sure if it is even possible to change the behaviour of these built-in classes.

So my next idea was to derive a class from each numerical type and overload the operators there. This creates the hassle of converting from double[] to MyDoubleArray and back, which reduces the benefit of me doing less typing.

Also, I don’t really want to have to repeat a load of almost identical functionality for every numeric type. This lead to my next idea of a generic operator class. In fact, someone else had also had this idea: there’s a generic operator class in Jon Skeet’s MiscUtil library.

This gives you a method-like prefix syntax for operations, e.g.

double sum = Operator<double>.Add(3.5, -2.44); // 1.06

The trouble is, since the array types don’t support addition, you can’t just do something like

double[] sum = Operator<double[]>.Add(a, b);  // Throws InvalidOperationException

I’ve run out of ideas. Can you think of anything that will work?

  • 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-12T19:20:15+00:00Added an answer on May 12, 2026 at 7:20 pm

    You should probably create a Vector class that internally wraps an array and overloads the arithmetic operators. There’s a decent matrix/vector code library here.

    But if you really need to operate on naked arrays for some reason, you can use LINQ:

    var a1 = new double[] { 0, 1, 2, 3 };
    var a2 = new double[] { 10, 20, 30, 40 };
    
    var sum = a1.Zip( a2, (x,y) => Operator<double>.Add( x, y ) ).ToArray();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent much time looking for a special package that could run the
I have spent way too much time looking for the files owner icon to
Have spend quite some time to learn Sencha touch, just like an evaluation about
guys! I am very interested in game programming.I have spent some time learning C++
I've spent too much time trying to get this to work on IE 7.
I spend most of my time plotting data, but unfortunately I haven't found a
I spend most of my time in C# and am trying to figure out
I feel like I spend a lot of time writing code in Python, but
I'll spend some time writing some (raw) SQL statements in Android (e.g. for creating
Im new to GUI programming, and haven't done much OOP. Im working on a

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.