standard vector

Hi guys

 

How can i construct the standard vector e[i]. Where e[i] is a 4 dimensional vector with 0 in all entries apart from the ith which has ?

Doug Meade's picture

the four standard unit vectors in R4

Do you want the four standard unit vectors in R4, or a general vector e[i]?

If it's the former, then you can use the following simple loop:

for i from 1 to 4 do
  e[i] := Vector(4,j->`if`(i=j,1,0));
end do;

I am not aware of any way to work with a standard unit vector indexed by the general i. You can probably define how this vector interacts with other vectors, but I'll not go there right now.

I hope this is helpful, or that others will show where I am wrong,

Doug

---------------------------------------------------------------------
Douglas B. Meade  <><
Math, USC, Columbia, SC 29208  E-mail: mailto:meade@math.sc.edu       
Phone:  (803) 777-6183         URL:    http://www.math.sc.edu/~meade/

variations

You can take advantage of the Vector constructor and do

for i to 4 do
    e[i] := Vector(4, {i=1});
end do;

Another possibility is to use LinearAlgebra:-UnitVector:

for i to 4 do
   e[i] := LinearAlgebra:-UnitVector(i,4);
end do;
acer's picture

indexing function, or not

It is possible to create a Vector which uses a built-in indexing function that accomplishes this. An indexing function is a smart-access mechanism for Vectors and Matrices, as well as a control on what may be assigned into the object.

This is what LinearAlgebra:-UnitVector() can produce, which Joe illustrated. The code below shows how the Vector() constructor can also produce the same objects.

> for i to 4 do
> e[i] := Vector(4,'shape'='unit'[i]);
> end do:

> e[3];
                                      [0]
                                      [ ]
                                      [0]
                                      [ ]
                                      [1]
                                      [ ]
                                      [0]

> lprint(e[2]);
Vector[column](4,{},datatype = anything,storage = empty,
order = Fortran_order, shape = [unit[2]])

On the one hand, this can be useful if the dimension is very large (>>4 say) and one wishes to keep memory allocation down. That's because such Vectors have "empty" storage, yet produce the right values whenever any element of them is accessed or used. On the other hand, it may incur a slight but measurable extra time efficiency cost to access them many times, due to the overhead of calling the indexing function for each access. That's a typical storage versus speed dichotomy.

Calling LinearAlgebra:-UnitVector() with the 'compact'=false option will produce these objects without any indexing function, and with full explicit storage.

> for i to 4 do
> e[i] := LinearAlgebra:-UnitVector(i,4,'compact'=false);
> end do:

> e[3];
                                      [0]
                                      [ ]
                                      [0]
                                      [ ]
                                      [1]
                                      [ ]
                                      [0]
 
> lprint(e[2]);
Vector[column](4,{(2) = 1},datatype = anything,
storage = rectangular,order = Fortran_order,shape = [])

That takes more memory (not really relevant for dimension 4, naturally), but access will be faster. Also, objects created in this way do not have restrictions on what might be subsequently assigned into their entries, which may or may not be important to one's tasks. I like this method.

One can also create row Vectors.

> LinearAlgebra:-UnitVector['row'](2,4,'compact'=false);
                                 [0, 1, 0, 0]

And it's also possible to create these objects with full non-empty storage as well as an indexing function. Eg,

> V := Vector(4,'shape'='unit'[3],'storage'='rectangular'):

> lprint(V);
Vector[column](4,{(3) = 1},datatype = anything,
storage = rectangular,order = Fortran_order,
shape = [unit[3]])

This is the least useful, since it takes more memory while still disallowing any assignment that disobeys the indexing function. There's not much point to having both those aspects hold for the same object.

> V[4]:=17;
Error, unit vector can only have one non-zero entry

That error message isn't as clear as it could be.

So, all in all there is a lot of flexibility. I find that the LinearAlgebra package's UnitVector provides the most straightforward way to get all the desirable functionality.

acer

John Fredsted's picture

Vector of Vectors

You can also define them as follows:

e := Vector(4,i -> Vector(4,j -> `if`(i = j,1,0)));

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}