Vector Interval
- To: mathgroup at smc.vnet.net
- Subject: [mg33358] Vector Interval
- From: Christopher Purcell <purcell at drea.dnd.ca>
- Date: Sun, 17 Mar 2002 05:33:10 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
I would like to extend Interval to handle vector arguments for use in
computational geometry. The following code demonstrates
what I have in mind, but it would be much more elegant to overload Interval
to accept the vector arguments, than define a new function. My attempts to
do that failed, because IntervalUnion broke in the process. Any hints
would be appreciated.
(*
*******************************************************************************************************************************************
*)
(* Extending Interval to handle vector arguments. The vectors min and max
point to the min and max points of a pixel in 2 dimensions or a voxel in
3D. *)
SequenceOfPairsOfVectorsQ::usage=
"SequenceOfPairsOfVectorsQ[args__] returns True if args is a Sequence \
of 2 or more lists of Length 2, whose entries are all themselves lists, and
all \
these entries have the same Length, and False otherwise. ";
SequenceOfPairsOfVectorsQ[args__] := If[Union[Length /@ {args}] == {2} &&
SameQ[Length /@ Flatten[{args}, 1]]&& Length[{args}]>1&&
Union[Map[Head,{args}[[1]]]]==={List}, True, False];
VectorInterval::usage=
"VectorInterval[{min_List, max_List}] is a vector generalization of the
Interval function, which returns a list of Intervals of length equal to the
length of min and max, where min and max are n-dimensional vectors (of the
same length). VectorInterval[{min1, max1}, {min2, max2}, ... ] represents the
union of the vector ranges min1 to max1, min2 to max2, .... where the
min1,max1, ... are vectors (of the same length).";
VectorInterval[{min_List, max_List}] := If[Length[min] == Length[max],
(Interval[{#1[[1]], #1[[2]]}] & ) /@ Transpose[{min, max}],
HoldForm[Interval[{min}, {max}]]];
VectorInterval[args__List]:=If[SequenceOfPairsOfVectorsQ[args],
Map[IntervalUnion[Apply[Sequence,#]]&,Transpose[Map[VectorInterval[#]&,{args}]]],HoldForm[VectorInterval[args]]];
(*
*******************************************************************************************************************************************
*)
In[10]:=
VectorInterval[{{0, 0}, {1, 1}}] (* 2d case - this is ok *)
Out[10]=
{Interval[{0, 1}], Interval[{0, 1}]}
In[11]:=
VectorInterval[{{0, 0, 0}, {1, 1, 1}}](* 3d case - this is ok *)
Out[11]=
{Interval[{0, 1}], Interval[{0, 1}], Interval[{0, 1}]}
In[11]:=
VectorInterval[{{0, 0, 0}, {1, 1, 1}}, {{1, 1, 1}, {2, 2, 2}},{{2, 2, 2},
{4, 2, 2}}] (* a sequence of 3d vector intervals works as expected *)
Out[11]=
{Interval[{0, 4}], Interval[{0, 2}], Interval[{0, 2}]}
Christopher J. Purcell
DREA, 9 Grove St., PO Box 1012
Dartmouth NS Canada B2Y 3Z7
Tel 902-426-3100 x389, Fax: 902-426-9654
E-mail: purcell at drea.dnd.ca