On Mon, Sep 18, 2017 at 4:34 AM, Ralf Hemmecke <[email protected]> wrote:
>>
>> tmp := 0$%
>
> Ooops. No. If you intend to use tmp later, you shouldn't use 0, but
> initialize it to a copy of 0.
Yes, it should be "tmp : % := new(dim, 0)$Rep" instead.
>> subtractIfCan(u:%, v:%):Union(%,"failed") ==
>> for i in 1..dim repeat
>> (c := subtractIfCan(qelt(u, i)$Rep, qelt(v,i)$Rep)) case
>> "failed" =>
>> return "failed"
>
> Yes, that's the way it would be done with only the current
> "subtractIfCan" available. But in case of failure in the last component
> of the vector, one would have created a lot of Union(R, "failed")
> objects where only the R part would be used in the following qsetelt!
>
>> qsetelt!(tmp, i, c::R)$Rep
>
> That sounds like needless memory allocation.
Hmm, your following approach seems like to alloc this memory
on stack instead of heap, and potentially do the computation twice.
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.)
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.