Applying a linear transform to a std::vector<T>

Discussion in 'C++' started by mathieu, Apr 25, 2008.

  1. mathieu

    mathieu Guest

    Hi there,

    I am dealing with the following problem:

    I need to convert a std::vector<T> (where T can be any interget
    type: char, short, ushort) by applying a linear transform (a,b),
    following;

    output = a*input + b (a & b are floating point type)

    Since memory space is important, I am trying to divide the problem
    in subcases. Basically

    1. If a or b is float then the output vector need to be declared as
    vector<float> (input vector is at most 16bits integer type)

    2. Is a & b are integer I need to compute the min/max of the input
    scalar type, apply the transform and check the output interval to find
    which C type is the best match.

    Since this look like boilerplate code, I was wondering if there was
    anything I could reuse (other than just numeric_limits to find the min
    max). Even just the interval calculation is tricky, although in my
    case I can just cast eveyrthing to double since I am dealing with at
    most 32bits calculation.

    thanks for comments,
    -Mathieu
    mathieu, Apr 25, 2008
    #1
    1. Advertising

  2. On Fri, 25 Apr 2008 15:52:01 +0200, mathieu <>
    wrote:

    > Hi there,
    >
    > I am dealing with the following problem:
    >
    > I need to convert a std::vector<T> (where T can be any interget
    > type: char, short, ushort) by applying a linear transform (a,b),
    > following;
    >
    > output = a*input + b (a & b are floating point type)
    >
    > Since memory space is important, I am trying to divide the problem
    > in subcases. Basically
    >
    > 1. If a or b is float then the output vector need to be declared as
    > vector<float> (input vector is at most 16bits integer type)
    >
    > 2. Is a & b are integer I need to compute the min/max of the input
    > scalar type, apply the transform and check the output interval to find
    > which C type is the best match.
    >
    > Since this look like boilerplate code, I was wondering if there was
    > anything I could reuse (other than just numeric_limits to find the min
    > max). Even just the interval calculation is tricky, although in my
    > case I can just cast eveyrthing to double since I am dealing with at
    > most 32bits calculation.
    >
    > thanks for comments,
    > -Mathieu



    May be you should use std::valarray.
    David Côme, Apr 25, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    1,111
    Roedy Green
    Nov 15, 2005
  2. Anonymous
    Replies:
    20
    Views:
    4,295
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    725
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    946
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page