Skip to main content
Removed unnecessary UpValues
Source Link
VF1
  • 4.7k
  • 24
  • 31

I believe that what you're looking for is some data structure Vector which has some list defining direction and some scalar which in part defines magnitude.

Here you go:

Vector[a_List] := Vector[1, a]
Vector[b_, _]["scalar"] := b
Vector[_, a_List]["vector"] := a
Vector /: (b_ Vector[c_, a_List]) := Vector[c b, a]
a = 3 Vector[{1, 1, 0}];
b = 2 Vector[.3, {3, 2, 0}];

If you want to "extract" the scalars, then use magicfunction:

Vector /: magicfunction[a__Vector, z_] := 
   Times @@ (#["scalar"] &) /@ List[a] z @@ (#["vector"] &) /@ List[a]

For instance:

magicfunction[a, b, Cross]
(* {0., 0., -1.8} *)
magicfunction[a, b, Hold]
(* 1.8 Hold[{1, 1, 0}, {3, 2, 0}] *)

In order to get the regular vector back, just use Normal. Make sure you have a copy of your Vector, however, as this transformation will lose the information about the scalar.

Vector /: Normal[Vector[b_, a_List]] := b a
Normal[a]
(* {3, 3, 0} *)

I believe that what you're looking for is some data structure Vector which has some list defining direction and some scalar which in part defines magnitude.

Here you go:

Vector[a_List] := Vector[1, a]
Vector[b_, _]["scalar"] := b
Vector[_, a_List]["vector"] := a
Vector /: (b_ Vector[c_, a_List]) := Vector[c b, a]
a = 3 Vector[{1, 1, 0}];
b = 2 Vector[.3, {3, 2, 0}];

If you want to "extract" the scalars, then use magicfunction:

Vector /: magicfunction[a__Vector, z_] := 
   Times @@ (#["scalar"] &) /@ List[a] z @@ (#["vector"] &) /@ List[a]

For instance:

magicfunction[a, b, Cross]
(* {0., 0., -1.8} *)
magicfunction[a, b, Hold]
(* 1.8 Hold[{1, 1, 0}, {3, 2, 0}] *)

In order to get the regular vector back, just use Normal. Make sure you have a copy of your Vector, however, as this transformation will lose the information about the scalar.

Vector /: Normal[Vector[b_, a_List]] := b a
Normal[a]
(* {3, 3, 0} *)

I believe that what you're looking for is some data structure Vector which has some list defining direction and some scalar which in part defines magnitude.

Here you go:

Vector[a_List] := Vector[1, a]
Vector[b_, _]["scalar"] := b
Vector[_, a_List]["vector"] := a
Vector /: (b_ Vector[c_, a_List]) := Vector[c b, a]
a = 3 Vector[{1, 1, 0}];
b = 2 Vector[.3, {3, 2, 0}];

If you want to "extract" the scalars, then use magicfunction:

magicfunction[a__Vector, z_] := 
   Times @@ (#["scalar"] &) /@ List[a] z @@ (#["vector"] &) /@ List[a]

For instance:

magicfunction[a, b, Cross]
(* {0., 0., -1.8} *)
magicfunction[a, b, Hold]
(* 1.8 Hold[{1, 1, 0}, {3, 2, 0}] *)

In order to get the regular vector back, just use Normal. Make sure you have a copy of your Vector, however, as this transformation will lose the information about the scalar.

Vector /: Normal[Vector[b_, a_List]] := b a
Normal[a]
(* {3, 3, 0} *)
Clarified Normal conversion
Source Link
VF1
  • 4.7k
  • 24
  • 31

I believe that what you're looking for is some data structure Vector which has some list defining direction and some scalar which in part defines magnitude.

Here you go:

Vector[a_List] := Vector[1, a]
Vector[b_, _]["scalar"] := b
Vector[_, a_List]["vector"] := a
Vector /: (b_ Vector[c_, a_List]) := Vector[c b, a]
a = 3 Vector[{1, 1, 0}];
b = 2 Vector[.3, {3, 2, 0}];

If you want to "extract" the scalars, then use magicfunction:

Vector /: magicfunction[a__Vector, z_] := 
   Times @@ (#["scalar"] &) /@ List[a] z @@ (#["vector"] &) /@ List[a]

For instance:

magicfunction[a, b, Cross]
(* {0., 0., -1.8} *)
magicfunction[a, b, Hold]
(* 1.8 Hold[{1, 1, 0}, {3, 2, 0}] *)

In order to get the regular vector back, just use Normal. Make sure you have a copy of your Vector, however, as this transformation will lose the information about the scalar.

Vector /: Normal[Vector[b_, a_List]] := b a
Normal[a]
(* {3, 3, 0} *)

I believe that what you're looking for is some data structure Vector which has some list defining direction and some scalar which in part defines magnitude.

Here you go:

Vector[a_List] := Vector[1, a]
Vector[b_, _]["scalar"] := b
Vector[_, a_List]["vector"] := a
Vector /: (b_ Vector[c_, a_List]) := Vector[c b, a]
a = 3 Vector[{1, 1, 0}];
b = 2 Vector[.3, {3, 2, 0}];

If you want to "extract" the scalars, then use magicfunction:

Vector /: magicfunction[a__Vector, z_] := 
   Times @@ (#["scalar"] &) /@ List[a] z @@ (#["vector"] &) /@ List[a]

For instance:

magicfunction[a, b, Cross]
(* {0., 0., -1.8} *)
magicfunction[a, b, Hold]
(* 1.8 Hold[{1, 1, 0}, {3, 2, 0}] *)

In order to get the regular vector back, just use Normal

Vector /: Normal[Vector[b_, a_List]] := b a
Normal[a]
(* {3, 3, 0} *)

I believe that what you're looking for is some data structure Vector which has some list defining direction and some scalar which in part defines magnitude.

Here you go:

Vector[a_List] := Vector[1, a]
Vector[b_, _]["scalar"] := b
Vector[_, a_List]["vector"] := a
Vector /: (b_ Vector[c_, a_List]) := Vector[c b, a]
a = 3 Vector[{1, 1, 0}];
b = 2 Vector[.3, {3, 2, 0}];

If you want to "extract" the scalars, then use magicfunction:

Vector /: magicfunction[a__Vector, z_] := 
   Times @@ (#["scalar"] &) /@ List[a] z @@ (#["vector"] &) /@ List[a]

For instance:

magicfunction[a, b, Cross]
(* {0., 0., -1.8} *)
magicfunction[a, b, Hold]
(* 1.8 Hold[{1, 1, 0}, {3, 2, 0}] *)

In order to get the regular vector back, just use Normal. Make sure you have a copy of your Vector, however, as this transformation will lose the information about the scalar.

Vector /: Normal[Vector[b_, a_List]] := b a
Normal[a]
(* {3, 3, 0} *)
Source Link
VF1
  • 4.7k
  • 24
  • 31

I believe that what you're looking for is some data structure Vector which has some list defining direction and some scalar which in part defines magnitude.

Here you go:

Vector[a_List] := Vector[1, a]
Vector[b_, _]["scalar"] := b
Vector[_, a_List]["vector"] := a
Vector /: (b_ Vector[c_, a_List]) := Vector[c b, a]
a = 3 Vector[{1, 1, 0}];
b = 2 Vector[.3, {3, 2, 0}];

If you want to "extract" the scalars, then use magicfunction:

Vector /: magicfunction[a__Vector, z_] := 
   Times @@ (#["scalar"] &) /@ List[a] z @@ (#["vector"] &) /@ List[a]

For instance:

magicfunction[a, b, Cross]
(* {0., 0., -1.8} *)
magicfunction[a, b, Hold]
(* 1.8 Hold[{1, 1, 0}, {3, 2, 0}] *)

In order to get the regular vector back, just use Normal

Vector /: Normal[Vector[b_, a_List]] := b a
Normal[a]
(* {3, 3, 0} *)