As mentionned in the topic title, I try to apply my own sorting functions to a set of elements via the Sort command. I make here a quick presentation of the subject before.
1) Method
We will remain in 2D throughout the description of the problem
1-1) Treatment of the component
We consider a component of a vector as a real number.
To sort two real, it is necessary to compare them.
The particularity of the sort that I want to apply is that the comparison depend of a specific criterion c
which can take two values ("max"
and "min"
).
In this case, to qualify the order relation between the two numbers, we won't say that :
- something1
is superior/inferior to something2
but,
- something1
dominate/don't dominate something2
.
You can either :
- Wanting to maximize the value of the component. c = "max"
- Wanting to minimize the value of the component. c = "min"
Example :
With :
Group n°1 : a1 = 5
and a2 = 3
Group n°2 : a3 = -2
and a4 = -3
If
c = "max"
We havea1 > a2
, soa1
dominatesa2
.
We havea3 > a4
, soa1
dominatesa2
.If
c = "min"
We always havea1 > a2
, but nowa2
dominatesa1
.
We always havea3 > a4
, but nowa1
dominatesa2
.
Remarks :
- We can not decide in case of equality, so no number dominates another.
- The criterion c
allows you to change the sort direction.
1-2) Treatment of the vector
The functions that I want to use, are based on the principle of dominance (translated from french), and its variations.
Principle 1 :
We say that a vector J
dominates a vector K
if :
- J
is at least as good as K
in all component.
- J
is strictly better than K
in at least one component.
Examples :
Take two vector such as :
v1 = {2, 5}
;
v2 = {1, 6}
;
We have several possibilities to compare this two vector according to the value of the criterion c
for each of these components.
v1[[1]] > v2[[1]]
v1[[2]] < v2[[2]]
Case 1 : {"max","max"}
We can't say if a vector dominate an other.
Case 2 : {"max","min"}
v1
dominate v2
.
Case 3 : {"min","max"}
v2
dominate v1
.
Case 4 : {"min","min"}
We can't say if a vector dominate an other.
So we can see here, that the sort of this two vector depend directly of the value of c
.
2) My code
CompO[w_, x_, y_] :=
Block[
{Res},
Res = If[
x == y,
{0},
If[w == "max", Ordering[{x, y}, -1], Ordering[{x, y}, 1]]]
];
BoolCompVDS[w__, x__, y__] :=
Block[
{step1, step2, step3, Res},
step1 = MapThread[CompO, {w, x, y}];
step2 = DeleteCases[Union@step1, 0, Infinity];
step3 =
If[
Count[step2, _Integer, Infinity] == 1,
Flatten@step2,
{0}
];
Res = If[step3 == {1}, True, False]
];
To understand :
We need to compare two vector :
v1 = {2, 5};
v2 = {1, 6};
The rule (for this example) are the following :
- We want to maximize the first component.
- We want to minimize the second component.
According to the principle 1 :
v1[[1]] > v2[[1]] so v1[[1]] dominate v2[[1]]
(we want to maximize)v1[[2]] < v2[[2]] so v1[[2]] dominate v2[[2]]
(we want to minimize)
So v1
dominate v2
and :
BoolCompVDS[{"max","min"}, v1, v2]
give "True"
3) Problem
Thanks to the post of Mr. Wizards, my original question can be formulated like this :
Why the use of Sort
with my personal function does not produce a strict order as may be the case with the use of the Norm function (for example) ?
As seen in the post of Mr Wizards, the Sort command is designed to work with strict order relations.
I would write an answer for an update on sorting with function based on dominance relations soon.
{a, b}
and{c, d}
should be ordered? I find part 2 of your question incredibly hard to comprehend. As I think I have gathered, the ordering{{a, b}, {c, d}}
is correct ifa < c
andb > d
, but for the example functions#^2
and#^3
this is never simultaneously realized. $\endgroup$BoolCompVDS
defined with three parameters of variable length i.e.BoolCompVDS[w__, x__, y__]
and what do you expect this to do? $\endgroup$