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 7885059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:57:49+00:00 2026-06-03T04:57:49+00:00

I am using C to do some scientific computing, and need to move around

  • 0

I am using C to do some scientific computing, and need to move around a lot of vectors.

I’ve defined some basic Multiply and Add functions of the form

add(int size, double *out, double *x, double *y)

But for complex operations the code quickly becomes long and hard to read.

Is it possible to define inline operators (V1 + V2) ? Or any general best practices that will make it easier to spot check code for math errors? Perhaps some #define shenanigans?

  • 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-06-03T04:57:50+00:00Added an answer on June 3, 2026 at 4:57 am

    At first I thought this was a C++ question for some reason!

    If you can use C++ you might be able to use an STL array (where the size is a template parameter, not a stored value).

    It might look something like this:

    std::array<double, 7> a = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
    std::array<double, 7> b = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
    auto c = a + b;
    auto d = a + b + c;
    

    And you can define operator overload for operator+. E.g:

    #include <array>
    // ...
    // (version 1) returns a new copy/temporary
    template <class T, size_t size>
    std::array<T, size> operator+(const std::array<T, size>& a, 
                                  const std::array<T, size>& b)
    {
        std::array<T, size> c;
        // add them however you want; simple version:
        for (size_t i = 0; i < size; ++i)
        {
            c[i] = a[i] + b[i];
        }
        return c;
    }
    
    // (version 2) no temporaries via rvalue ref/move semantic
    template <class T, size_t size>
    std::array<T, size>&& operator+(std::array<T, size>&& a, 
                                    const std::array<T, size>& b)
    {
        for (size_t i = 0; i < size; ++i)
        {
            a[i] += b[i];
        }
        return std::move(a);
    }
    

    so that:

    auto c = a + b;       // invokes version 1
    auto d = a + b + c;   // invokes version 1 for (b+c) 
                          // and then version 2 for a+(temp)
    

    so you use at most one temporary in any chain of operations.

    The memory layout of a std::array should be the same as a native array, so you should be able to “inject” this code quite easily into your existing program (by (ab)using casts, typedefs or preprocessor) if you want to touch a minimal amount of existing code.

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

Sidebar

Related Questions

I've inherited the maintenance of some scientific computing using Parallel Python on a cluster.
I'm using C++ to perform scientific simulation on some things. At this moment, due
I am trying to write an OpenGL visualization program for some scientific data using
I am plotting some curves using twin-axis and also scientific notation. I have set
I've tried using some of the assembly tutorials around the web, and most of
Using some of the methods, I am able to check the orientations to which
Assuming I'm using some graphic API which allows me to draw bezier curves by
I am using some NHibernate 1.2 code with NHibernate 2.0 and its giving me
I am using some third party library to connect to a server via async
I'm using some code from here to determine when determining when the last finger

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.