Mario wrote:
Hello dear Oz users,
In my script Sum must be sum of scalar product Z.I x L.I where I={1..3}
How can I do it quickly and efficient. In my problem I will use
matrices with dimension from 5x5 to 30x30
First trick: (Z.1 x L.1 + ... + Z.n x L.n) is equivalent to the scalar
product of Zs x Ls, where Zs is the concatenation of the vectors Z1,
..., Z.n (similar for Ls). Here is the function Concat that
concatenates a list of lists:
fun {Concat Ls}
case Ls of L|Lr then {Append L {Concat Lr}} else nil end
end
Zs={Concat {Record.toList Z}}
Ls={Concat {Record.toList L}}
For the product itself, it depends whether both terms are constrained or
not. If both are constrained, you have to use FD.sumCN which implements
a polynomial constraint:
{FD.sumCN {List.zip Ls Zs fun {$ Li Zi} Li#Zi end} '=:' Sum}
However in your example the vectors Li are constant. Therefore the
constraint is a linear one. FD.sumC will do the job:
% ===== Sample script ====
declare
L = l([5 13 7]
[9 6 4]
[7 8 6])
proc {Problem X}
Z = z({FD.list 3 0#1}
{FD.list 3 0#1}
{FD.list 3 0#1})
%% makes life easier
Zs={Concat {Record.toList Z}}
Ls={Concat {Record.toList L}}
Sum={FD.decl} % = { } <-- My question - Sum = Z1xL1 + Z2xL2 + Z3xL3
{FD.sumC Ls Zs '=:' Sum}
in
X = x(z1:Z sum:Sum)
% ... constraints matrix Z ...
{For 1 3 1
proc {$I}
{FD.distribute ff Z.I}
end}
Bad idea here. You should distribute on all variables at once
(including Sum). Hopefully, we already have Zs:
{FD.distribute ff Sum|Zs}
end
{Explorer.all Problem}
Cheers,
raph
_________________________________________________________________________________
mozart-users mailing list
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users