On 09/18/2017 01:13 AM, oldk1331 wrote:
> Hmm, your following approach seems like to alloc this memory
> on stack instead of heap, and potentially do the computation twice.

Yes. Double computation *is* a problem, especially when the function
canSubtract?$R (on the coefficients is more time consuming than an
immediate allocation of the array for the result.

> I don't like this change because the most used case, NNI, has
> a good solution; on the other hand, I think Lisp's "Multiple Values
> return" can avoid computation twice. (I'll dig it later.)

Oh. I don't know Lisp so much. But when you speak of "multiple values",
then you probably want to return the result vector as well. So there is
need to allocate memory. Sounds like memory allocation in the original code.

Most probably there is no generic solution to this problem, how to
implement subtractIfCan in DirectProduct(dim, R) heavily depends on R.
That is why I would not add a default implementation of subtract or
subtractIfCan in CancellationAbelianMonoid. Each single domain like
DirectProduct, should decide itself, under which conditions one
implementation is to be preferred over the other. That's a hard enough
question for the programmer, becaus in general FriCAS does not offer
some documentation about time complexity of implemented functions.

Ralf

> I agree with the name "subtract".
> 
>> I would suggest to introduce two new function canSubtract? and subtract.
>> (The names are, of course, open for discussion.)
>>
>> CancellationAbelianMonoid() : Category == AbelianMonoid with
>>   --operations
>>   canSubtract?: (%, %) -> Boolean
>>     ++ canSubtract?(x, y) retruns true if that is an element z such
>>     ++ that \spad{z+y=x} and false otherwise.
>>   subtract: (%, %) -> %
>>     ++ subtract(x, y) retruns an element z such that \spad{z+y=x} if
>>     ++ such an element exists, i.e., if canSubtract?(x, y) is true.
>>     ++ The behaviour of that function is unspecified if no such element
>>     ++ exists, i.e., if canSubtract?(x, y) is false.
>>   subtractIfCan : (%,%) -> Union(%,"failed")
>>     ++ subtractIfCan(x, y) returns an element z such that \spad{z+y=x}
>>     ++ or "failed" if no such element exists.
>>
>> In DirectProduct we could have something like
>>
>>   QE(x, i) ==> qelt(x, i)$Rep
>>   canSubtract?(u: %, v: %): Boolean ==
>>     for in in 1 .. dim repeat
>>       if not canSubtract(QE(u, i), QE(v, i)) then return false
>>     true
>>   subtractIfCan(u: %, v: %): Union(%, "failed") ==
>>     canSubtract?(u, v) => subtract(u, v)::Union(%, "failed")
>>     "failed"
>>   -- Note the the following requires canSubtract(u, v) to be true.
>>   subtract(u: %, v: %): % ==
>>     w: % :=  new(dim, 0)$Vector(R)
>>     for in in 1 .. dim repeat
>>       qsetelt!(w, i, subtract(QE(u, i), QE(v,i)))$Rep
>>     return w
>>
>>> One might expect that 'subtract' never fails. I think we should choose
>>> between 'subtractIf' or 'subtractCan'.
>>
>> Well, I still opt for "subtract" in connection with "canSubtract?" and
>> "subtractIfCan".
>>
>> Ralf
> 

-- 
You received this message because you are subscribed to the Google Groups 
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to