[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

ValArray

jacob navia

4/12/2011 1:10:00 PM

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
7 Answers

Mark Bluemel

4/12/2011 2:19:00 PM

0

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.

Ben Bacarisse

4/12/2011 4:46:00 PM

0

jacob navia <jacob@spamsink.net> 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.

jacob navia

4/12/2011 4:53:00 PM

0

Le 12/04/11 18:46, Ben Bacarisse a écrit :
> jacob navia<jacob@spamsink.net> 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

4/12/2011 4:56:00 PM

0

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

Keith Thompson

4/12/2011 8:20:00 PM

0

jacob navia <jacob@spamsink.net> 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) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

jacob navia

4/12/2011 9:05:00 PM

0

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.

Ian Collins

4/13/2011 10:10:00 PM

0

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