Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Columns.cs
1using System.Numerics;
8
10{
16 {
26 : base(new ScriptNode[] { Vector }, argumentTypes1Normal, Start, Length, Expression)
27 {
28 }
29
39 public Columns(ScriptNode Vector, ScriptNode SecondDimension, int Start, int Length, Expression Expression)
40 : base(new ScriptNode[] { Vector, SecondDimension },
41 new ArgumentType[] { ArgumentType.Vector, ArgumentType.Scalar },
43 {
44 }
45
49 public override string FunctionName => nameof(Columns);
50
54 public override string[] DefaultArgumentNames => new string[] { "M" };
55
63 {
64 if (Arguments.Length == 1 && Arguments[0] is IMatrix M)
65 return new DoubleNumber(M.Columns);
66
67 if (!(Arguments[0] is IVector V))
68 throw new ScriptRuntimeException("Expected vector in first argument.", this);
69
70 int SecondDimension;
71
72 if (Arguments.Length == 2)
73 {
74 SecondDimension = (int)Expression.ToDouble(Arguments[1].AssociatedObjectValue);
75 if (SecondDimension <= 0)
76 throw new ScriptRuntimeException("Invalid secondary dimension.", this);
77 }
78 else
79 SecondDimension = V.Dimension;
80
82 {
83 double[] Values = DoubleVector.Values;
84 int c = Values.Length;
85 int i, j;
86 double[,] Elements = new double[c, SecondDimension];
87 double Value;
88
89 for (i = 0; i < c; i++)
90 {
91 Value = Values[i];
92 for (j = 0; j < SecondDimension; j++)
93 Elements[i, j] = Value;
94 }
95
96 return new DoubleMatrix(Elements);
97 }
98 else if (V is ComplexVector ComplexVector)
99 {
100 Complex[] Values = ComplexVector.Values;
101 int c = Values.Length;
102 int i, j;
103 Complex[,] Elements = new Complex[c, SecondDimension];
104 Complex Value;
105
106 for (i = 0; i < c; i++)
107 {
108 Value = Values[i];
109 for (j = 0; j < SecondDimension; j++)
110 Elements[i, j] = Value;
111 }
112
113 return new ComplexMatrix(Elements);
114 }
115 else if (V is BooleanVector BooleanVector)
116 {
117 bool[] Values = BooleanVector.Values;
118 int c = Values.Length;
119 int i, j;
120 bool[,] Elements = new bool[c, SecondDimension];
121 bool Value;
122
123 for (i = 0; i < c; i++)
124 {
125 Value = Values[i];
126 for (j = 0; j < SecondDimension; j++)
127 Elements[i, j] = Value;
128 }
129
130 return new BooleanMatrix(Elements);
131 }
132 else
133 {
134 int c = V.Dimension;
135 int i, j;
136 IElement[,] Elements = new IElement[c, SecondDimension];
137
138 i = 0;
139 foreach (IElement Value in V.VectorElements)
140 {
141 for (j = 0; j < SecondDimension; j++)
142 Elements[i, j] = Value;
143
144 i++;
145 }
146
147 return new ObjectMatrix(Elements);
148 }
149 }
150 }
151}
Class managing a script expression.
Definition: Expression.cs:41
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:5110
Creates a matrix whose columns have elements of the same value, each defined by the corresponding ele...
Definition: Columns.cs:16
override string FunctionName
Name of the function
Definition: Columns.cs:49
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Columns.cs:62
Columns(ScriptNode Vector, ScriptNode SecondDimension, int Start, int Length, Expression Expression)
Creates a matrix whose columns have elements of the same value, each defined by the corresponding ele...
Definition: Columns.cs:39
override string[] DefaultArgumentNames
Default Argument names
Definition: Columns.cs:54
Columns(ScriptNode Vector, int Start, int Length, Expression Expression)
Creates a matrix whose columns have elements of the same value, each defined by the corresponding ele...
Definition: Columns.cs:25
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes1Normal
One scalar parameter.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Complex[] Values
Vector element values.
double[] Values
Vector element values.
Definition: DoubleVector.cs:48
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
Basic interface for matrices.
Definition: IMatrix.cs:7
Basic interface for vectors.
Definition: IVector.cs:9
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9