public abstract class Vector extends Value.Structured implements Value.Scannable
nElts()
the number of
top-level elements, elt(i)
, and
wt(i)
the weight of elt(i). The default
assumptions are that ∀ i wt(i)=1
(see constWt()
), and that, if applicable,
elt(i).AoM()
may vary with i
(see constAoM()
. Also see
Type.VECTOR
, Value.Tuple
and
Matrix
.Modifier and Type | Class and Description |
---|---|
class |
Vector.Derived
Class Derived can be useful when making a modified Vector.
|
static class |
Vector.Doubles
A Vector containing doubles (
Cts Values) can
probably arrange efficient storage and operations. |
static class |
Vector.GP
A simple, general purpose (GP) implementation of
class
Vector . |
static class |
Vector.Ints
A Vector containing
Int Values can
probably arrange efficient storage and operations. |
class |
Vector.Slice
Vector.Slice, a subVector of 'this' Vector; see
slice . |
static class |
Vector.Strings
A Vector containing Strings, that is, having
Chars elements. |
class |
Vector.Weighted
'This' Vector (data-set?) of elements, each element
explicitly
weighted , somehow. |
Value.Atomic, Value.Bool, Value.Char, Value.Chars, Value.Cts, Value.Defer, Value.Discrete, Value.Enum, Value.Inc_Or, Value.Int, Value.Lambda, Value.List, Value.Maybe, Value.Option, Value.Real, Value.Scannable, Value.Structured, Value.Triv, Value.Tuple
Modifier and Type | Field and Description |
---|---|
static Vector |
empty
The empty Vector with no elements at all.
|
Constructor and Description |
---|
Vector() |
Modifier and Type | Method and Description |
---|---|
Vector |
addMore(Vector.Slice s)
|
Vector |
addMore(Vector v,
int lo,
int hi)
It is impossible, an error, to add more than zero elements
(of 'v'==this) to 'this' Vector as they are all already there,
but to a
Slice is possible. |
double |
AoM(int i)
Return this.elt(i).AoM() — assuming this is a
Vector of Cts.
|
Vector |
append()
For 'this' non-empty Vector (data-set?) of Vectors,
append them all, that is
flatten k dimensions to (k-1) dimensions. |
Vector |
append(Vector v2)
Append Vectors 'this' and 'v2', of the same type!
|
Matrix |
as_1xn()
Make a "row-Vector", i.e., a 1×n-
Matrix ,
of 'this' Vector (regardless of the element type). |
Matrix |
as_nx1()
Make a "column Vector", i.e., a n×1-
Matrix ,
of 'this' Vector (regardless of the element type). |
Matrix |
asDiagonal(Value z)
Make a square, diagonal
Matrix of 'this' Vector,
using 'z' for all off-diagonal elements. |
Matrix |
asMatrix()
Provided 'this' is a rectangular Vector of Vectors, return it
as the equivalent
Matrix ; also see Matrix.asMatrix() . |
Q |
asQ()
Make 'this' 4D Vector of Cts a
quaternion
in the obvious way. |
Q |
asQrotn()
Make 'this' 3D Vector of Cts,
quaternion rotate(q) . |
Value |
bOp(int op,
Value v)
Binary operator, 'op', on 'this' Vector, and 'v', that is apply
op element-wise returning a Vector of results. |
protected java.lang.String |
closes()
|
Vector |
col(int c)
Provided 'this' is a Vector of
Value.Structured ,
return the c-th column, as a Vector. |
Vector |
cols(int[] cs)
Provided 'this' is a Vector of
Value.Tuple , return
the Vector of Tuples made up of the columns specified by cs. |
Vector |
combine(boolean add,
Vector.Slice v)
|
int |
compareTo(Value v)
Compare 'this' Vector to v, returning -ve (<),
0 (=), or +ve (>).
|
double |
constAoM()
By default, return −1 (any negative) indicating the elements' AoMs
(if appropriate) may vary from element to element.
|
double |
constNlAoM()
If
constAoM() is >0, return
its negative log, otherwise an error. |
double |
constWt()
By default, return 1.0 indicating that every element has that
weight . |
static Vector |
csv(boolean trimHead,
boolean trimTail,
char separator,
Type.Tuple rowType,
int skipLines,
java.io.InputStream inp)
Input a data-set of "comma-"separated values from a given InputStream
(often a
FileInputStream ). |
static Vector |
csv(Type.Tuple rowType,
java.io.InputStream inp)
csv(...) using some common default parameter values. |
Vector |
delete(Vector.Slice s)
|
Vector |
delete(Vector v,
int lo,
int hi)
Delete elements [lo, hi) of 'v' from 'this'; it requires
(and checks) that
|
double |
dot(Vector v)
The dot- (inner-) product of Vectors of Cts, 'this' and 'v'.
|
static Vector.Doubles |
doubles(double[] xs)
Convenience function : double[] → Vector; note AoM=0.
|
static Vector.Doubles |
doubles(double[] xs,
double AoM)
Convenience function : double[] × AoM → Vector,
where every element of the Vector has the same
AoM (do not confuse it with
doubles(double,double[]) . |
static Vector.Doubles |
doubles(double nlAoM,
double[] xs)
Convenience function : nlAoM × double[] → Vector,
where we know the nlAoM of the Vector as a whole, but not
of each elt(i).
|
Vector |
drop(int m)
Return all but the first 'm' elements of 'this' Vector;
see
take(int) and slice(int, int) . |
Vector |
drop(int lo,
int hi)
Drop elements [lo,hi) of 'this' Vector, lo inclusive,
hi exclusive.
|
Vector |
dropLast(int m)
Return all but the last 'm' elements of 'this' Vector;
see
slice(int, int) . |
abstract Value |
elt(int i)
Element 'i' of 'this' Vector.
|
Value |
elt(int r,
int c)
Provided 'this' is a Vector of
Value.Structured ,
return the element at row 'r', |
Vector.Derived |
elts(int[] ps)
Return a sub-Vector of 'this', being made up of the
positions, ps.
|
Type |
eltType()
The type of all(!) elements of 'this' Vector.
|
Value |
foldl(Function fc)
|
Value |
foldl(Function fc,
Value z)
foldl (fold-left, reduce) 'this' Vector with curried Function 'fc'
and zero (identity) 'z', in effect
|
Value |
foldr(Function fc)
|
Value |
foldr(Function fc,
Value z)
foldr (fold-right, reduce) 'this' Vector with curried Function
'fc' and zero (identity) 'z', in effect
|
static Vector |
fromScannable(Value.Scannable sv)
Make a Vector from the given Scannable Value sv.
|
static Vector |
ints(int[] ns)
Convenience function, ints : int[] → Vector.
|
static Vector |
ints(Type.Discrete et,
int[] ns)
Convenience function to make a Vector with elements of
Discrete Type et, and
integer codes, ns. |
boolean |
isRectangular()
Provided 'this' is a Vector of Vectors, is it
rectangular? That is, is it non-"jagged"? Also see
isSquare() , and Matrix . |
boolean |
isSquare()
Provided 'this' is a Vector of Vectors, is it square?
Also see
isRectangular() . |
static void |
main(java.lang.String[] argv)
main() allows Vector to be (slightly) tested, in isolation.
|
Vector |
map(Function f)
Apply Function 'f' to each element of 'this' Vector,
returning a Vector of results.
|
Q |
match(Vector t)
Find the rotation (as a
quaternion ), by some angle about
some axis through the origin, that minimises the sum of squared errors
between the Vectors of 3D Vectors of Cts, 'this' and 't'. |
protected void |
merge(int lo,
int hi,
int[] src,
int[] tgt,
java.util.Comparator<Value> cmp)
MergeSort tgt[lo,hi) on the basis of
|
int |
n(int i)
Return this.elt(i).n() — assuming this is a
Vector of Int.
|
int |
nCols()
Provided 'this' is a rectangular Vector of
Structured , which property is
checked within, return its number of columns, otherwise throw an error. |
boolean |
nearlySymmetric()
Provided 'this' is a Vector of Vectors of Cts, return
nearlySymmetric (Maths.epsilon ). |
boolean |
nearlySymmetric(double epsilon)
Provided 'this' is a Vector of Vectors of Cts, is it square
&& (nearly-)symmetric? That is, are x[r][c] and x[c][r]
nearlyEqual
for all r and c? |
abstract int |
nElts()
The number of elements (rows) in 'this' Vector.
|
int |
nElts(int row)
Provided 'this' is a Vector of
Value.Structured ,
return nElts() of elt(row). |
double |
nlAoM()
The total -ve log AoM of the whole Vector (if appropriate); it
looks at
constAoM() and acts accordingly. |
double |
nlAoM(int i)
Return this.elt(i).nlAoM() — assuming this is a
Vector of Cts.
|
double |
nlAoM(int lo,
int hi)
Return the sum
nlAoM of elements [lo, hi). |
Value.Cts |
norm_Cts()
|
double |
norm()
The Euclidean norm, ||v||=√(v.v), of 'this'
Vector of Cts, that is, √
sumSq() . |
Vector |
normalised()
|
protected Vector |
normalised1()
Return 'this' Vector of Cts
normalised
by making a Vector of new (scaled) elements. |
protected Vector |
normalised2()
Return 'this' Vector of Cts
normalised
but without copying the (scaled) elements. |
protected java.lang.String |
opens()
|
static Vector |
repeat(Value v,
int n)
The Vector
|
Vector |
rotate(double t,
Vector n)
Given an angle, 't', in radians, and an axis, 'n', rotate 'this'
3D Vector by t about n.
|
Vector |
rotate(Q q)
As specified by the
quaternion , 'q', rotate 'this'
3D Vector by an angle about an axis. |
int |
rowLength()
Provided this is a Vector of Vectors, if it is
rectangular return its row length, else a negative int.
|
Vector |
scaled(double s)
Return 'this' Vector scaled by a factor, s.
|
int[] |
shape()
For a Vector of Vector of Vec..., return the "nElts()" of the leading
(hyper-)rectangular dimensions, e.g., a simple Vector would give [m],
an m×n
Matrix [m,n], etc., and
a jagged Vector of Vectors [m]. |
static Vector |
singleton(Value e)
Return a Vector consisting of a single element, 'e'.
|
Vector |
slice(int lo,
int hi)
Return a
Slice (section), |
Vector.Derived |
sorted()
Return 'this' Vector, sorted on
Value.comparator . |
Vector.Derived |
sorted(java.util.Comparator<Value> cmp)
Return 'this' Vector sorted on 'cmp'.
|
Vector.Derived |
sorted(int col)
Return 'this' Vector sorted on column 'col'.
|
static Vector.Strings |
strings(java.lang.String[] ss)
Convenience function : String[] → Vector.
|
double |
sumSq()
Provided 'this' is a Vector of Cts, return the sum of squares,
|
Vector |
take(int m)
Return the first 'm' elements of 'this' Vector;
see
drop(int) and slice(int, int) . |
Vector |
takeLast(int m)
Return the last 'm' elements of 'this' Vector; see
slice(int, int) . |
Vector |
times(Matrix M)
'this' Vector of Cts, v, as a row-Vector, times
Matrix M,
as in |
Series |
toSeries()
|
Type |
type()
|
Value[] |
uniqueElts()
Return an array containing the unique elements of
'this' Vector in ascending order.
|
Value.Tuple |
unzip()
unzip 'this' Vector of Tuples into a Tuple
of Vectors.
|
Value.Tuple |
unzip(int nCol)
unzip 'this' Vector of Tuples each of (must be) nCol elements.
|
Value |
uOp(int op)
Unary operator, 'op', on 'this' Vector, that is apply
op
element-wise, returning a new Vector of results. |
static Vector.GP |
values(Value[] vs)
Convenience function, values : Value[] →
GP . |
Vector.Weighted |
weight(double wt)
Return a Vector, based on the data in 'this' Vector, but with
weights adjusted × 'wt' ≥ 0.
|
Vector.Weighted |
weight(double[] wts)
Return a Vector, based on the data in 'this' Vector, but with weights
adjusted × wts[i].
|
Vector |
weight(Vector v)
Re-weight 'this' Vector adjusting weights by
× v.elt(i).x().
|
Vector |
weightLike(double w)
Re-weight 'this' Vector setting all
weights equal to w ≥ 0.
|
Vector |
weightLike(Vector v)
Re-weight 'this' Vector setting weights, wt(i),
equal to v.wt(i).
|
double |
wt(int i)
wt(i), the weight, of
elt(i) in
'this' data-set (Vector); this default is 1.0. |
int |
wtMedian()
Return the index, m, of the median element on the basis of
weights . |
double |
wtNlAoM()
The total -ve log AoM of the whole Vector (if appropriate),
taking into account element
weights . |
double |
wtNlAoM(int lo,
int hi)
Return the total weighted
nlAoM of
elements [lo, hi). |
double |
wts()
|
double |
wts(int lo,
int hi)
Return the total
weights of elements [lo, hi). |
double |
x(int i)
Return this.elt(i).x() — assuming this is a
Vector of Cts.
|
Vector |
zip(Vector v)
zip 'this' Vector with another Vector 'v',
of the same length, to produce a Vector of pairs as in,
|
static Vector |
zip(Vector[] cols)
zip several Vectors to make a Vector of Tuples.
|
AoM, elts, frth, fst, print, separator, snd, thrd, toString
public static final Vector empty
protected java.lang.String opens()
opens
in class Value.Structured
protected java.lang.String closes()
closes
in class Value.Structured
public abstract int nElts()
wts()
.nElts
in class Value.Structured
public abstract Value elt(int i)
elt
in class Value.Structured
public int n(int i)
public double x(int i)
public Type eltType()
public double wt(int i)
elt(i)
in
'this' data-set (Vector); this default is 1.0. We
may have fractional weights
for
mixture
-modelling, say, >1 for multiple copies
of a datum, and zero to turn a datum "off". Note that wt(i),
and constWt()
must be consistent!
For some operations on Vector, such as append, it is pretty
clear what wt(i) should be in the result, but not for some
others, such as zip, in which case you get something plausible.
Also see wts()
.public double constWt()
weight
. Return a negative to indicate that weights may
vary. Used by wts()
, wtNlAoM()
, etc..
(Also see constAoM()
.)public double constAoM()
nlAoM()
and wtNlAoM()
.
Also see constNlAoM()
and
constWt()
.public double constNlAoM()
constAoM()
is >0, return
its negative log, otherwise an error.public double nlAoM()
constAoM()
and acts accordingly.
This is particularly for pr(v)
and nlPr(v)
where v is a Cts Vector. Also see wtNlAoM()
.nlAoM
in class Value.Structured
public double nlAoM(int lo, int hi)
public double AoM(int i)
public double nlAoM(int i)
public double wts()
public double wts(int lo, int hi)
weights
of elements [lo, hi).public int wtMedian()
public double wtNlAoM()
weights
.
This is particularly for nlLH(ss)
of a
Cts data-set and so on. It looks at constWt()
and at constAoM()
and acts accordingly.
Also see nlAoM()
.public double wtNlAoM(int lo, int hi)
nlAoM
of
elements [lo, hi).public Vector.Derived elts(int[] ps)
cols(cs)
and Value.Tuple.elts(int[])
.public Value[] uniqueElts()
public static Vector singleton(Value e)
public Series toSeries()
toSeries
in interface Value.Scannable
public static Vector fromScannable(Value.Scannable sv)
Series
is used up and cannot be "rewound"
but a Scannable can be "re- toSeries() -ed".public Vector.Derived sorted()
Value.comparator
.
Also see sorted(Comparator)
.public Vector.Derived sorted(int col)
public Vector.Derived sorted(java.util.Comparator<Value> cmp)
sorted()
.protected void merge(int lo, int hi, int[] src, int[] tgt, java.util.Comparator<Value> cmp)
public Vector.Weighted weight(double[] wts)
weight(double)
.public Vector.Weighted weight(double wt)
weight(Vector)
.public Vector weight(Vector v)
weight(double[])
.public Vector weightLike(double w)
public Vector weightLike(Vector v)
weight(double[])
.public int compareTo(Value v)
public double norm()
sumSq()
.public Value.Cts norm_Cts()
norm
as a
Value.Cts
with nlAoM() being the Vector's
divided by nElts(). Also see normalised
.public double sumSq()
norm()
.public double dot(Vector v)
public Vector times(Matrix M)
Matrix
M,
as in public Vector scaled(double s)
public Vector normalised()
protected Vector normalised1()
normalised
by making a Vector of new (scaled) elements.protected Vector normalised2()
normalised
but without copying the (scaled) elements.public Vector slice(int lo, int hi)
Slice
(section), take
, drop
and Vector.Slice.slice(int, int)
.public Vector take(int m)
drop(int)
and slice(int, int)
.public Vector takeLast(int m)
slice(int, int)
.public Vector drop(int m)
take(int)
and slice(int, int)
.public Vector drop(int lo, int hi)
public Vector dropLast(int m)
slice(int, int)
.public Vector combine(boolean add, Vector.Slice v)
public Vector delete(Vector.Slice s)
public Vector delete(Vector v, int lo, int hi)
Vector.Slice.delete(Vector,int,int)
.
addMore(v,lo,hi)
are used to
create subsets of the elements of a Vector, e.g., sub-data-sets.public Vector addMore(Vector.Slice s)
public Vector addMore(Vector v, int lo, int hi)
Slice
is possible. Requires
(and checks) that delete(v,lo,hi)
are used to
create subsets of the elements of a Vector, e.g., sub-data-sets.public Vector append()
append
them all, that is
flatten k dimensions to (k-1) dimensions.public Value uOp(int op)
op
element-wise, returning a new Vector of results.public Value bOp(int op, Value v)
op
element-wise returning a Vector of results.public static Vector zip(Vector[] cols)
Value.Tuple.zip()
.
Also see zip(v)
and unzip
.public Value.Tuple unzip()
public Value.Tuple unzip(int nCol)
unzip()
allows for 'this'=empty.public Vector map(Function f)
public Value foldl(Function fc, Value z)
foldr(fx,z)
.public Value foldr(Function fc, Value z)
foldl(fc,z)
.public int[] shape()
Matrix
[m,n], etc., and
a jagged Vector of Vectors [m].public int nElts(int row)
public Value elt(int r, int c)
Value.Structured
,
return the element at row 'r', public Vector col(int c)
Value.Structured
,
return the c-th column, as a Vector. E.g., col(c) is used by
Independent
to select a column of a multivariate data-set.
Also see cols(cs)
, columns plural.public Vector cols(int[] cs)
Value.Tuple
, return
the Vector of Tuples made up of the columns specified by cs. Also
see col(c)
and elts(ps)
.public int nCols()
Structured
, which property is
checked within, return its number of columns, otherwise throw an error.
Also see Matrix.nCols()
and Matrix.nElts(int)
.public Matrix asMatrix()
Matrix
; also see Matrix.asMatrix()
.
(Just a "type" change and no significant extra space is consumed.)public Matrix as_1xn()
Matrix
,
of 'this' Vector (regardless of the element type).
Note that wt(0), for the single row, is 1.0.public Matrix as_nx1()
Matrix
,
of 'this' Vector (regardless of the element type).public Matrix asDiagonal(Value z)
Matrix
of 'this' Vector,
using 'z' for all off-diagonal elements.public boolean isRectangular()
isSquare()
, and Matrix
.public int rowLength()
public boolean isSquare()
isRectangular()
.public boolean nearlySymmetric(double epsilon)
nearlyEqual
for all r and c?public boolean nearlySymmetric()
nearlySymmetric
(Maths.epsilon
).public static Vector.GP values(Value[] vs)
GP
.
Also see the 2D Matrix.values(Value[][])
.public static Vector ints(int[] ns)
Matrix.ints(int[][])
.public static Vector ints(Type.Discrete et, int[] ns)
Discrete Type
et, and
integer codes, ns.public static Vector.Doubles doubles(double nlAoM, double[] xs)
Model
's (Vector of) parameters, say. (And do not confuse
it with doubles(double[],double)
.public static Vector.Doubles doubles(double[] xs, double AoM)
AoM
(do not confuse it with
doubles(double,double[])
. Also see the 2D
Matrix.doubles(double[][],double)
.public static Vector.Doubles doubles(double[] xs)
Matrix.doubles(double[][])
.public static Vector.Strings strings(java.lang.String[] ss)
public Vector rotate(double t, Vector n)
rotate(q)
.public Vector rotate(Q q)
quaternion
, 'q', rotate 'this'
3D Vector by an angle about an axis. (Is called by
rotate(t,n)
.)public Q asQrotn()
quaternion
rotate(q)
. Also see asQ()
.public Q asQ()
quaternion
in the obvious way.public Q match(Vector t)
quaternion
), by some angle about
some axis through the origin, that minimises the sum of squared errors
between the Vectors of 3D Vectors of Cts, 'this' and 't'.
Also see
[www].public static Vector csv(boolean trimHead, boolean trimTail, char separator, Type.Tuple rowType, int skipLines, java.io.InputStream inp)
FileInputStream
). Tuple Type
'rowType' gives the Type of one row, that is each Variable (column).
Presently, allowed Variable Types are
Discrete
(Int, Bool, Enum, ...),
Cts
and
CHARS
(String).
The separator is often ',' but can be (almost) any character.
Leading and/or trailing white space can be trimmed from values.
Zero or more leading lines can be skipped, e.g., a title or a comment.
Values, and rows, may not be split across input lines.
Makes use of Series.Lines
and
Series.Separator
.public static Vector csv(Type.Tuple rowType, java.io.InputStream inp)
csv(...)
using some common default parameter values.
'inp' may be a FileInputStream
.public static void main(java.lang.String[] argv)