Can one be found programmatically? The function I'm compiling, indicesOfMin, gives the indices of the minimum value(s) of a real-valued list, the parameter, in a single traversal (which is why I'm using this instead of Ordering or Sort).
indicesOfMin = Compile[{{list, _Real, 1}},
Module[{bag = Internal`Bag[Most[{0}]], min = 1.7976931348623157*^308, sub = 0.},
Do[If[(sub = list[[i]] - min) < 0, min = list[[i]]; bag = Internal`Bag[Most[{0}]]; Internal`StuffBag[bag, i];,
If[sub == 0, Internal`StuffBag[bag, i]]],
{i, 1, Length[list], 1}];
Internal`BagPart[bag, All]
]];
As you can see, I'm using C++'s positive infinity value for my first setting
of min, but I don't even know if that's the language this will be compiled to.
Potential issues:
- I understand I can use the first value in
listformin, but that takes extra code and I would like to avoid doing so because it also creates a precondition that the list is at least one element long. - I don't know what language this will be compiled to in advance (the function is part of a package, and the person running it may not have a
Ccompiler, so the maximum field value might be wrong.
doubletype. I think the minimum/maximum depends on how your CPU works, not the language. Compile can compile to a special byte code used by Mma, or it can compile to C. Both use the same type of machine number. – Szabolcs Feb 06 '13 at 18:25doubles regardless of your CPU? – VF1 Feb 06 '13 at 18:32$MaxMachineNumberbut I don't know enough about the internals to vouch for it. You need to figure out a way to throw it in to theCompileto avoidMainEvaluateperhaps withBlock– ssch Feb 06 '13 at 18:35