public void GetScalarTest() { ScriptRuntime qsruntime = Qs.Scripting.QsContext.CreateRuntime(); ScriptEngine QsEngine = qsruntime.GetEngine("Qs"); QsTensor t = QsEngine.Execute("<|3 4; 3 1 | 9 8; 4 7 |>") as QsTensor; var sv = t.GetScalar(0, 0, 0); Assert.AreEqual("3<1>", sv.ToShortString()); t = QsEngine.Execute("<| <|3 4; 3 1 | 9 8; 4 7|> | <|30 4; 3 1 | 9 18; 4 7|> |>") as QsTensor; // getting scalar from 4th rank tensor. sv = t.GetScalar(1, 1, 0, 1); Assert.AreEqual("18<1>", sv.ToShortString()); t = QsEngine.Execute("<| <| <|3 4; 3 1 | 9 8; 4 7|> | <|30 4; 3 1 | 9 18; 4 7|> |> | <| <|3 4;4 1 | 9 8; 4 7|> | <|30 403<L>; 302 1 | 19 180; 40 700|> |> |>") as QsTensor; // getting scalar from 5th rank tensor. sv = t.GetScalar(1, 1, 0, 0, 1); Assert.AreEqual("403<L>", sv.ToShortString()); }
/// <summary> /// This is a tricky functions /// it returns Vector if components are Scalars. /// Matrix if components are Vectors /// </summary> /// <param name="fromIndex"></param> /// <param name="toIndex"></param> /// <returns></returns> public QsValue QsValueElements(int fromIndex, int toIndex) { if (this.Parameters.Length > 0) { List <string> ProcessedElements = new List <string>(); #region symbolic representation if (fromIndex > toIndex) { for (int e_ix = fromIndex; e_ix >= toIndex; e_ix--) { var se = GetElement(e_ix); // s[n](x) ..> $x*x^n+$n # symbolic variables shouldn't be changed ($x, $n) we should take care. // first preserve the symbolic variable with the same index name that we are going to change. string se_text = se.ElementDeclaration.Replace("$" + this.SequenceIndexName, "`"); // replace the index name with the se_text = se_text.Replace(this.SequenceIndexName, e_ix.ToString(CultureInfo.InvariantCulture)); // get back the symbolic se_text = se_text.Replace("`", "$" + this.SequenceIndexName); if (!string.IsNullOrEmpty(SequenceRangeStartName)) { se_text = se_text.Replace("$" + SequenceRangeStartName, "`"); se_text = se_text.Replace(SequenceRangeStartName, StartIndex.ToString(CultureInfo.InvariantCulture)); se_text = se_text.Replace("`", "$" + SequenceRangeStartName); } if (!string.IsNullOrEmpty(SequenceRangeEndName)) { se_text = se_text.Replace("$" + SequenceRangeEndName, "`"); se_text = se_text.Replace(SequenceRangeEndName, EndIndex.ToString(CultureInfo.InvariantCulture)); se_text = se_text.Replace("`", "$" + SequenceRangeEndName); } // replace the parameters in declaration with the same foreach (var param in this.Parameters) { se_text = se_text.Replace("$" + param.Name, "`"); se_text = se_text.Replace(param.Name, "$" + param.Name); se_text = se_text.Replace("`", "$" + param.Name); } ProcessedElements.Add(se_text); } } else { for (int e_ix = fromIndex; e_ix <= toIndex; e_ix++) { var se = GetElement(e_ix); string se_text = se.ElementDeclaration.Replace("$" + this.SequenceIndexName, "`"); se_text = se_text.Replace(this.SequenceIndexName, e_ix.ToString(CultureInfo.InvariantCulture)); se_text = se_text.Replace("`", "$" + this.SequenceIndexName); if (!string.IsNullOrEmpty(SequenceRangeStartName)) { se_text = se_text.Replace("$" + SequenceRangeStartName, "`"); se_text = se_text.Replace(SequenceRangeStartName, StartIndex.ToString(CultureInfo.InvariantCulture)); se_text = se_text.Replace("`", "$" + SequenceRangeStartName); } if (!string.IsNullOrEmpty(SequenceRangeEndName)) { se_text = se_text.Replace("$" + SequenceRangeEndName, "`"); se_text = se_text.Replace(SequenceRangeEndName, EndIndex.ToString(CultureInfo.InvariantCulture)); se_text = se_text.Replace("`", "$" + SequenceRangeEndName); } // replace the parameters with names foreach (var param in this.Parameters) { se_text = se_text.Replace("$" + param.Name, "`"); se_text = se_text.Replace(param.Name, "$" + param.Name); se_text = se_text.Replace("`", "$" + param.Name); } ProcessedElements.Add(se_text); } } var ee = QsEvaluator.CurrentEvaluator.SilentEvaluate(ProcessedElements[0]); QsValue Total; if (ee is QsScalar) { Total = new QsVector(System.Math.Abs(toIndex - fromIndex) + 1); foreach (string pel in ProcessedElements) { ((QsVector)Total).AddComponent((QsScalar)QsEvaluator.CurrentEvaluator.SilentEvaluate(pel)); } } else if (ee is QsVector) { Total = new QsMatrix(); foreach (string pel in ProcessedElements) { ((QsMatrix)Total).AddVector((QsVector)QsEvaluator.CurrentEvaluator.SilentEvaluate(pel)); } } else if (ee is QsMatrix) { Total = new QsTensor(); foreach (string pel in ProcessedElements) { ((QsTensor)Total).AddMatrix((QsMatrix)QsEvaluator.CurrentEvaluator.SilentEvaluate(pel)); } } else { throw new QsException("This is enough, no more than matrix values please."); } #endregion return(Total); } QsValue firstElement = (QsValue)GetElementValue(fromIndex); if (firstElement is QsScalar) { //return vector QsVector Total = new QsVector(System.Math.Abs(toIndex - fromIndex) + 1); #region Numerical Representation Total.AddComponent((QsScalar)firstElement); if (toIndex >= fromIndex) { for (int i = fromIndex + 1; i <= toIndex; i++) { Total.AddComponent((QsScalar)GetElementValue(i)); } } else { for (int i = fromIndex - 1; i >= toIndex; i--) { Total.AddComponent((QsScalar)GetElementValue(i)); } } #endregion return(Total); } else if (firstElement is QsVector) { //return vector QsMatrix Total = new QsMatrix(); Total.AddVector((QsVector)firstElement); if (toIndex >= fromIndex) { for (int i = fromIndex + 1; i <= toIndex; i++) { Total.AddVector((QsVector)GetElementValue(i)); } } else { for (int i = fromIndex - 1; i >= toIndex; i--) { Total.AddVector((QsVector)GetElementValue(i)); } } return(Total); } else if (firstElement is QsMatrix) { throw new NotImplementedException(); } else { throw new NotSupportedException(); } }