ValArray

Discussion in 'C Programming' started by jacob navia, Apr 12, 2011.

  1. jacob navia

    jacob navia Guest

    In the context of the C Containers library I am now implementing "ValArray"

    These containers store the basic numeric types (integers and floats)
    of the language.

    They support the basic operations that can be done with those types.

    The question is:

    ValArray A,B,C;

    // ...

    C = iValArrayDouble.SumTo(B,A);

    This is the equivalent of B += A in C++.

    The question is, what happens when Size(A) != Size(B). C++
    leaves that as "undefined", probably crashing. My library
    throws an error and does nothing.

    An alternative solution would be to use the length of B for the
    operation in question, extending the shorter array if necessary
    or discarding results that go beyond the length of B.

    Operation Identity elem.
    + 0
    - 0
    * 1
    / 1
    & 1
    | 0
    % 1 (for integers only)
    Xor ????

    What do you think?

    Which alternative is better?

    jacob
    jacob navia, Apr 12, 2011
    #1
    1. Advertising

  2. jacob navia

    Mark Bluemel Guest

    On 04/12/2011 02:10 PM, jacob navia wrote:
    > In the context of the C Containers library I am now implementing "ValArray"
    >
    > These containers store the basic numeric types (integers and floats)
    > of the language.
    >
    > They support the basic operations that can be done with those types.
    >
    > The question is:
    >
    > ValArray A,B,C;
    >
    > // ...
    >
    > C = iValArrayDouble.SumTo(B,A);
    >
    > This is the equivalent of B += A in C++.
    >
    > The question is, what happens when Size(A) != Size(B). C++
    > leaves that as "undefined", probably crashing. My library
    > throws an error and does nothing.
    >
    > An alternative solution would be to use the length of B for the
    > operation in question, extending the shorter array if necessary
    > or discarding results that go beyond the length of B.
    >
    > Operation Identity elem.
    > + 0
    > - 0
    > * 1
    > / 1
    > & 1
    > | 0
    > % 1 (for integers only)
    > Xor ????
    >
    > What do you think?
    >
    > Which alternative is better?


    My immediate reaction, backed up by a quick look at the discussion of
    array arithmetic in MATLAB (just happened to find it early in Google),
    is to favour the former approach, with a separate set of operations for
    the case where you need to perform maths using an array and a single
    value (increment all elements by 1, calculate VAT at 20% on all
    elements, etc.).

    The implicit extension approach seems more likely to lead to errors.
    Mark Bluemel, Apr 12, 2011
    #2
    1. Advertising

  3. jacob navia <> writes:

    > In the context of the C Containers library I am now implementing "ValArray"
    >
    > These containers store the basic numeric types (integers and floats)
    > of the language.
    >
    > They support the basic operations that can be done with those types.
    >
    > The question is:
    >
    > ValArray A,B,C;
    >
    > // ...
    >
    > C = iValArrayDouble.SumTo(B,A);
    >
    > This is the equivalent of B += A in C++.


    This confuses me. What's C? If it means B += A, there would be no
    third sequence.

    > The question is, what happens when Size(A) != Size(B). C++
    > leaves that as "undefined", probably crashing. My library
    > throws an error and does nothing.
    >
    > An alternative solution would be to use the length of B for the
    > operation in question, extending the shorter array if necessary
    > or discarding results that go beyond the length of B.
    >
    > Operation Identity elem.
    > + 0
    > - 0
    > * 1
    > / 1
    > & 1
    > | 0
    > % 1 (for integers only)
    > Xor ????


    The identity for &, | and Xor is -1 converted to the width of whatever
    integer type is involved. However, I may have missed something you are
    getting at because Xor is written ^ and I'd expect the restriction to
    apply to other operations and not just %. The identity for % is not 1.
    There are lots of identities for % (at least for positive integers) but
    none representable in the type of the operand since. For some type T,
    one choice would be Tmax + 1.

    If these are listed in order to show how to extend a short array, why
    not just say that the elements of B beyond the length of A remain
    unchanged?

    > What do you think?


    I'd look into providing a function to do the work rather than have SumTo
    and MulTo and all the rest. For one thing, how can you know what
    operations people will want to perform?

    [By the way, it is likely this post will duplicate what others have
    written by now, but I forgot to send it before getting distracted by
    other tasks and I don't want to waste the typing so I will post it
    without my usual check.]

    <snip>
    --
    Ben.
    Ben Bacarisse, Apr 12, 2011
    #3
  4. jacob navia

    jacob navia Guest

    Le 12/04/11 18:46, Ben Bacarisse a écrit :
    > jacob navia<> writes:
    >
    >> In the context of the C Containers library I am now implementing "ValArray"
    >>
    >> These containers store the basic numeric types (integers and floats)
    >> of the language.
    >>
    >> They support the basic operations that can be done with those types.
    >>
    >> The question is:
    >>
    >> ValArray A,B,C;
    >>
    >> // ...
    >>
    >> C = iValArrayDouble.SumTo(B,A);
    >>
    >> This is the equivalent of B += A in C++.

    >
    > This confuses me. What's C? If it means B += A, there would be no
    > third sequence.
    >


    Yes, C is unnecessary. Sorry.


    [snip]

    >
    > If these are listed in order to show how to extend a short array, why
    > not just say that the elements of B beyond the length of A remain
    > unchanged?
    >


    Of course, but I thought that I should justify that mathematically.

    >> What do you think?

    >
    > I'd look into providing a function to do the work rather than have SumTo
    > and MulTo and all the rest. For one thing, how can you know what
    > operations people will want to perform?
    >


    I provide "Apply" that will call a function for each
    element. But that would be overkill for a simple addition.

    The point of ValArray is to speed up vector operations by providing
    a nonaliased array. If we would call a function to do a simple
    addition that would kill performance.

    > [By the way, it is likely this post will duplicate what others have
    > written by now, but I forgot to send it before getting distracted by
    > other tasks and I don't want to waste the typing so I will post it
    > without my usual check.]
    >
    > <snip>


    Thanks for your input.
    jacob navia, Apr 12, 2011
    #4
  5. jacob navia

    jacob navia Guest

    Le 12/04/11 16:18, Mark Bluemel a écrit :

    > My immediate reaction, backed up by a quick look at the discussion of
    > array arithmetic in MATLAB (just happened to find it early in Google),
    > is to favour the former approach, with a separate set of operations for
    > the case where you need to perform maths using an array and a single
    > value (increment all elements by 1, calculate VAT at 20% on all
    > elements, etc.).



    True. I will provide two functions

    Array * array
    and
    Array * scalar
    >
    > The implicit extension approach seems more likely to lead to errors.


    For all binary operations I will provide a scalar variant. Thanks.

    For the time being I have a "strict" approach: arrays must be of the
    same length. If they are not I raise a signal (instead of crashing like
    the STL does now)

    But I wanted to explore other possibilities

    Thanks for your input
    jacob navia, Apr 12, 2011
    #5
  6. jacob navia <> writes:
    > In the context of the C Containers library I am now implementing "ValArray"
    >
    > These containers store the basic numeric types (integers and floats)
    > of the language.
    >
    > They support the basic operations that can be done with those types.
    >
    > The question is:
    >
    > ValArray A,B,C;
    >
    > // ...
    >
    > C = iValArrayDouble.SumTo(B,A);
    >
    > This is the equivalent of B += A in C++.
    >
    > The question is, what happens when Size(A) != Size(B). C++
    > leaves that as "undefined", probably crashing. My library
    > throws an error and does nothing.


    What exactly does "throws an error" mean?

    > An alternative solution would be to use the length of B for the
    > operation in question, extending the shorter array if necessary
    > or discarding results that go beyond the length of B.


    Or you could require the sizes to be equal, and provide a separate
    function that extends or truncates the result as needed, assuming that
    behavior is sufficiently useful. (I have no particular opinion on
    whether it is.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 12, 2011
    #6
  7. jacob navia

    jacob navia Guest

    Le 12/04/11 22:20, Keith Thompson a écrit :
    >>
    >> The question is, what happens when Size(A) != Size(B). C++
    >> leaves that as "undefined", probably crashing. My library
    >> throws an error and does nothing.

    >
    > What exactly does "throws an error" mean?
    >


    The library calls an error function (that can be replaced by the user)
    The default error function prints the function name where the error
    occurred, the interface name, and the type of error in the standard
    error stream (stderr).

    Normally the program would be aborted, but for the time being the
    default error function does return to its caller.
    jacob navia, Apr 12, 2011
    #7
  8. jacob navia

    Ian Collins Guest

    On 04/13/11 01:10 AM, jacob navia wrote:
    > In the context of the C Containers library I am now implementing "ValArray"
    >
    > These containers store the basic numeric types (integers and floats)
    > of the language.
    >
    > They support the basic operations that can be done with those types.
    >
    > The question is:
    >
    > ValArray A,B,C;
    >
    > // ...
    >
    > C = iValArrayDouble.SumTo(B,A);
    >
    > This is the equivalent of B += A in C++.
    >
    > The question is, what happens when Size(A) != Size(B). C++
    > leaves that as "undefined", probably crashing. My library
    > throws an error and does nothing.


    That looks like a sensible solution. I can't see any sense in operating
    with arrays of different lengths.

    --
    Ian Collins
    Ian Collins, Apr 13, 2011
    #8
    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. Glen Low
    Replies:
    3
    Views:
    968
    Glen Low
    Jun 26, 2003
  2. Marc Schellens

    Re: valarray resize

    Marc Schellens, Jul 29, 2003, in forum: C++
    Replies:
    5
    Views:
    1,272
    Marc Schellens
    Jul 30, 2003
  3. =?ISO-8859-1?Q?Christian_Brechb=FChler?=

    Enhancing valarray with "normal" arithmetic operators

    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Sep 12, 2003, in forum: C++
    Replies:
    6
    Views:
    945
    =?ISO-8859-1?Q?Christian_Brechb=FChler?=
    Sep 14, 2003
  4. Jim West

    const and valarray reference

    Jim West, Oct 23, 2003, in forum: C++
    Replies:
    4
    Views:
    1,124
    Victor Bazarov
    Oct 24, 2003
  5. Jim West
    Replies:
    2
    Views:
    486
    Jim West
    Dec 23, 2003
Loading...

Share This Page