Пример #1
0
 /// <summary>
 /// Gets whether the specified expression is an S4 object.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <returns><c>True</c> if the specified expression is an S4 object.</returns>
 public static bool IsS4(this SymbolicExpression expression)
 {
     if (expression == null)
     {
         throw new ArgumentNullException();
     }
     return(expression.GetFunction <Rf_isS4>()(expression.DangerousGetHandle()));
 }
Пример #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>
        /// Gets whether the specified expression is factor.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns><c>True</c> if the specified expression is factor.</returns>
        public static bool IsFactor(this SymbolicExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException();
            }
            var handle = expression.DangerousGetHandle();

            return(expression.GetFunction <Rf_isFactor>()(handle));
        }
Пример #4
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));
        }
Пример #5
0
        /// <summary>
        /// Converts the specified expression to a DynamicVector.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The DynamicVector. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static DynamicVector AsVector(this SymbolicExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException();
            }
            IntPtr coerced = expression.GetFunction <Rf_coerceVector>()(expression.DangerousGetHandle(), expression.Type);

            return(new DynamicVector(expression.Engine, coerced));
        }
Пример #6
0
 /// <summary>
 /// Gets whether the specified expression is list.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <returns><c>True</c> if the specified expression is list.</returns>
 public static bool IsList(this SymbolicExpression expression)
 {
     if (expression == null)
     {
         throw new ArgumentNullException();
     }
     // See issue 81. Rf_isList in the R API is NOT the correct thing to use (yes, hard to be more conter-intuitive)
     return(expression.Type == SymbolicExpressionType.List ||
            // ?is.list ==> "is.list returns TRUE if and only if its argument is a list or a pairlist of length > 0"
            (expression.Type == SymbolicExpressionType.Pairlist && expression.GetFunction <Rf_length>()(expression.DangerousGetHandle()) > 0));
 }
Пример #7
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);
        }