Пример #1
0
        /// <summary>
        /// Converts the specified expression to a RawVector.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The LogicalVector. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static RawVector AsRaw(this SymbolicExpression expression)
        {
            if (!expression.IsVector())
            {
                return(null);
            }
            IntPtr coerced = expression.GetFunction <Rf_coerceVector>()(expression.DangerousGetHandle(), SymbolicExpressionType.RawVector);

            return(new RawVector(expression.Engine, coerced));
        }
Пример #2
0
        /// <summary>
        /// Converts the specified expression to a CharacterVector.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The LogicalVector. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static CharacterVector AsCharacter(this SymbolicExpression expression)
        {
            if (!expression.IsVector())
            {
                return(null);
            }
            IntPtr coerced = IntPtr.Zero;

            if (expression.IsFactor())
            {
                coerced = expression.GetFunction <Rf_asCharacterFactor>()(expression.DangerousGetHandle());
            }
            else
            {
                coerced = expression.GetFunction <Rf_coerceVector>()(expression.DangerousGetHandle(), SymbolicExpressionType.CharacterVector);
            }
            return(new CharacterVector(expression.Engine, coerced));
        }
Пример #3
0
        /// <summary>
        /// Converts the specified expression to a RawMatrix.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The RawMatrix. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static RawMatrix AsRawMatrix(this SymbolicExpression expression)
        {
            if (!expression.IsVector())
            {
                return(null);
            }

            int rowCount    = 0;
            int columnCount = 0;

            if (expression.IsMatrix())
            {
                if (expression.Type == SymbolicExpressionType.RawVector)
                {
                    return(new RawMatrix(expression.Engine, expression.DangerousGetHandle()));
                }
                else
                {
                    rowCount    = expression.GetFunction <Rf_nrows>()(expression.DangerousGetHandle());
                    columnCount = expression.GetFunction <Rf_ncols>()(expression.DangerousGetHandle());
                }
            }

            if (columnCount == 0)
            {
                rowCount    = expression.GetFunction <Rf_length>()(expression.DangerousGetHandle());
                columnCount = 1;
            }

            IntPtr             coerced   = expression.GetFunction <Rf_coerceVector>()(expression.DangerousGetHandle(), SymbolicExpressionType.RawVector);
            var                dim       = new IntegerVector(expression.Engine, new[] { rowCount, columnCount });
            SymbolicExpression dimSymbol = expression.Engine.GetPredefinedSymbol("R_DimSymbol");
            var                matrix    = new RawMatrix(expression.Engine, coerced);

            matrix.SetAttribute(dimSymbol, dim);
            return(matrix);
        }