コード例 #1
0
ファイル: Math.cs プロジェクト: codyn-net/codyn-sharp
        public static string FunctionLookupById(Cdn.MathFunctionType type, out int arguments)
        {
            IntPtr raw_ret = cdn_math_function_lookup_by_id((int)type, out arguments);
            string ret     = GLib.Marshaller.Utf8PtrToString(raw_ret);

            return(ret);
        }
コード例 #2
0
ファイル: Math.cs プロジェクト: codyn-net/codyn-sharp
        public static bool FunctionIsVariable(Cdn.MathFunctionType type)
        {
            bool raw_ret = cdn_math_function_is_variable((int)type);
            bool ret     = raw_ret;

            return(ret);
        }
コード例 #3
0
ファイル: Math.cs プロジェクト: codyn-net/codyn-sharp
        public static bool FunctionIsCommutative(Cdn.MathFunctionType type, Cdn.StackArgs argdim)
        {
            bool raw_ret = cdn_math_function_is_commutative((int)type, argdim == null ? IntPtr.Zero : argdim.Handle);
            bool ret     = raw_ret;

            return(ret);
        }
コード例 #4
0
ファイル: Math.cs プロジェクト: codyn-net/codyn-sharp
        public static Cdn.MathFunctionType FunctionLookup(string name, out int arguments)
        {
            IntPtr native_name = GLib.Marshaller.StringToPtrGStrdup(name);
            int    raw_ret     = cdn_math_function_lookup(native_name, out arguments);

            Cdn.MathFunctionType ret = (Cdn.MathFunctionType)raw_ret;
            GLib.Marshaller.Free(native_name);
            return(ret);
        }
コード例 #5
0
ファイル: Math.cs プロジェクト: codyn-net/codyn-sharp
        public static unsafe bool FunctionGetStackManipulation(Cdn.MathFunctionType type, Cdn.StackArgs inargs, Cdn.StackArg outarg, out int extra_space)
        {
            IntPtr error   = IntPtr.Zero;
            bool   raw_ret = cdn_math_function_get_stack_manipulation((int)type, inargs == null ? IntPtr.Zero : inargs.Handle, outarg == null ? IntPtr.Zero : outarg.Handle, out extra_space, out error);
            bool   ret     = raw_ret;

            if (error != IntPtr.Zero)
            {
                throw new GLib.GException(error);
            }
            return(ret);
        }
コード例 #6
0
        private bool HasPriority(Cdn.MathFunctionType a, Cdn.MathFunctionType b)
        {
            OperatorSpec s1;
            OperatorSpec s2;

            if (!s_operatorSpecs.TryGetValue(a, out s1) || !s_operatorSpecs.TryGetValue(b, out s2))
            {
                return(false);
            }

            return(s1.Priority >= s2.Priority);
        }
コード例 #7
0
        private bool CanSparse(Cdn.MathFunctionType type)
        {
            switch (type)
            {
            case MathFunctionType.Divide:
            case MathFunctionType.Pow:
            case MathFunctionType.Power:
            case MathFunctionType.Emultiply:
            case MathFunctionType.Product:
            case MathFunctionType.Minus:
            case MathFunctionType.Plus:
            case MathFunctionType.Sqsum:
            case MathFunctionType.Sum:
            case MathFunctionType.Multiply:
            case MathFunctionType.Csum:
            case MathFunctionType.Rsum:
            case MathFunctionType.UnaryMinus:
            case MathFunctionType.PseudoInverse:
            case MathFunctionType.Transpose:
                return(true);
            }

            return(false);
        }
コード例 #8
0
ファイル: Context.cs プロジェクト: codyn-net/rawc
        public override string MathFunctionV(Cdn.MathFunctionType type, Tree.Node node)
        {
            var val = base.MathFunctionV(type, node);

            return("Cdn.Math." + val);
        }
コード例 #9
0
ファイル: Context.cs プロジェクト: codyn-net/rawc
        public override string MathFunction(Cdn.MathFunctionType type, int arguments)
        {
            var val = base.MathFunction(type, arguments);

            return("Cdn.Math." + val);
        }
コード例 #10
0
ファイル: Math.cs プロジェクト: codyn-net/codyn-sharp
 public static void FunctionExecute(Cdn.MathFunctionType type, Cdn.StackArgs argdim, Cdn.Stack stack)
 {
     cdn_math_function_execute((int)type, argdim == null ? IntPtr.Zero : argdim.Handle, stack == null ? IntPtr.Zero : stack.Handle);
 }
コード例 #11
0
 private static void AddSpec(Cdn.MathFunctionType type, int priority, bool leftAssociation)
 {
     s_operatorSpecs[type] = new OperatorSpec(type, priority, leftAssociation);
 }
コード例 #12
0
 public OperatorSpec(Cdn.MathFunctionType type, int priority, bool leftAssociation)
 {
     Type            = type;
     Priority        = priority;
     LeftAssociation = leftAssociation;
 }
コード例 #13
0
ファイル: Context.cs プロジェクト: codyn-net/rawc
        public virtual string MathFunctionV(Cdn.MathFunctionType type, Tree.Node node)
        {
            string name = Enum.GetName(typeof(Cdn.MathFunctionType), type);
            string val;

            switch (type)
            {
            case MathFunctionType.Atan2:
            case MathFunctionType.Pow:
            case MathFunctionType.Csign:
            case MathFunctionType.Hypot:
            case MathFunctionType.Sum:
            case MathFunctionType.Product:
            case MathFunctionType.Plus:
            case MathFunctionType.Modulo:
            case MathFunctionType.Emultiply:
            case MathFunctionType.Divide:
            case MathFunctionType.Minus:
            case MathFunctionType.Less:
            case MathFunctionType.LessOrEqual:
            case MathFunctionType.GreaterOrEqual:
            case MathFunctionType.Greater:
            case MathFunctionType.Equal:
            case MathFunctionType.And:
            case MathFunctionType.Or:
            case MathFunctionType.Nequal:
            case MathFunctionType.Negate:
            case MathFunctionType.Abs:
            case MathFunctionType.Acos:
            case MathFunctionType.Asin:
            case MathFunctionType.Atan:
            case MathFunctionType.Ceil:
            case MathFunctionType.Cos:
            case MathFunctionType.Cosh:
            case MathFunctionType.Exp:
            case MathFunctionType.Exp2:
            case MathFunctionType.Erf:
            case MathFunctionType.Floor:
            case MathFunctionType.Invsqrt:
            case MathFunctionType.Lerp:
            case MathFunctionType.Ln:
            case MathFunctionType.Log10:
            case MathFunctionType.Max:
            case MathFunctionType.Min:
            case MathFunctionType.Round:
            case MathFunctionType.Sin:
            case MathFunctionType.Sinh:
            case MathFunctionType.Sqrt:
            case MathFunctionType.Sqsum:
            case MathFunctionType.Tan:
            case MathFunctionType.Tanh:
            case MathFunctionType.Clip:
            case MathFunctionType.Cycle:
            case MathFunctionType.Transpose:
            case MathFunctionType.Sign:
            case MathFunctionType.Triu:
            case MathFunctionType.Tril:
            case MathFunctionType.Csum:
            case MathFunctionType.Rsum:
                val = String.Format("{0}_v", name.ToLower());
                break;

            case MathFunctionType.Vcat:
                return(Sparsify("vcat_v", node));

            case MathFunctionType.Power:
                val = "pow_v";
                break;

            case MathFunctionType.UnaryMinus:
                val = "uminus_v";
                break;

            case MathFunctionType.Multiply:
            {
                var d1 = node.Children[0].Dimension;
                var d2 = node.Children[1].Dimension;

                if (d1.Columns == d2.Rows && !(d1.IsOne || d2.IsOne))
                {
                    if (d1.Rows == 1 && d2.Columns == 1)
                    {
                        return(Sparsify("matrix_multiply", node));
                    }
                    else
                    {
                        return(Sparsify("matrix_multiply_v", node));
                    }
                }
                else
                {
                    val = "emultiply_v";
                }
                break;
            }

            case MathFunctionType.Diag:
            {
                var d1 = node.Children[0].Dimension;

                if (d1.Rows == 1 || d1.Columns == 1)
                {
                    val = "diag_v_v";
                }
                else
                {
                    val = "diag_v_m";
                }

                return(Sparsify(val, node));
            }

            case MathFunctionType.Slinsolve:
                return(Sparsify(String.Format("{0}_v", name.ToLower()), node));

            case MathFunctionType.Sltdl:
            case MathFunctionType.SltdlDinv:
            case MathFunctionType.SltdlDinvLinvt:
            case MathFunctionType.SltdlLinv:
            case MathFunctionType.SltdlLinvt:
                return(Sparsify(String.Format("{0}_v", name.ToLower()), node));

            default:
                throw new NotImplementedException(String.Format("The math function `{0}' is not supported...", name));
            }

            val = Sparsify(val, node);

            if (node.Children.Count == 2)
            {
                var d1 = node.Children[0].Dimension;
                var d2 = node.Children[1].Dimension;

                if ((d1.Columns == 1 || d2.Columns == 1) && d1.Rows == d2.Rows && d1.Rows != 1 && d1.Columns != d2.Columns)
                {
                    return(String.Format("{0}_cwise_{1}", val, d1.Columns == 1 ? "1_m" : "m_1"));
                }
                else if ((d1.Rows == 1 || d2.Rows == 1) && d1.Columns == d2.Columns && d1.Columns != 1 && d1.Rows != d2.Rows)
                {
                    return(String.Format("{0}_rwise_{1}", val, d1.Rows == 1 ? "1_m" : "m_1"));
                }
                else
                {
                    return(String.Format("{0}_{1}_{2}", val, d1.IsOne ? "1" : "m", d2.IsOne ? "1" : "m"));
                }
            }
            else if (node.Children.Count == 3)
            {
                var n1 = node.Children[0].Dimension.IsOne;
                var n2 = node.Children[1].Dimension.IsOne;
                var n3 = node.Children[2].Dimension.IsOne;

                return(String.Format("{0}_{1}_{2}_{3}", val, n1 ? "1" : "m", n2 ? "1" : "m", n3 ? "1" : "m"));
            }

            return(val);
        }
コード例 #14
0
ファイル: Context.cs プロジェクト: codyn-net/rawc
        public virtual string MathFunction(Cdn.MathFunctionType type, int arguments)
        {
            string name = Enum.GetName(typeof(Cdn.MathFunctionType), type);
            string val;

            switch (type)
            {
            case MathFunctionType.Abs:
            case MathFunctionType.Acos:
            case MathFunctionType.Asin:
            case MathFunctionType.Atan:
            case MathFunctionType.Atan2:
            case MathFunctionType.Ceil:
            case MathFunctionType.Cos:
            case MathFunctionType.Cosh:
            case MathFunctionType.Exp:
            case MathFunctionType.Exp2:
            case MathFunctionType.Floor:
            case MathFunctionType.Hypot:
            case MathFunctionType.Invsqrt:
            case MathFunctionType.Lerp:
            case MathFunctionType.Ln:
            case MathFunctionType.Log10:
            case MathFunctionType.Max:
            case MathFunctionType.Min:
            case MathFunctionType.Pow:
            case MathFunctionType.Round:
            case MathFunctionType.Sin:
            case MathFunctionType.Sinh:
            case MathFunctionType.Sqrt:
            case MathFunctionType.Tan:
            case MathFunctionType.Tanh:
            case MathFunctionType.Clip:
            case MathFunctionType.Cycle:
            case MathFunctionType.Modulo:
            case MathFunctionType.Erf:
            case MathFunctionType.Sign:
            case MathFunctionType.Csign:
            case MathFunctionType.Sum:
            case MathFunctionType.Product:
            case MathFunctionType.Triu:
            case MathFunctionType.Tril:
            case MathFunctionType.Diag:
            case MathFunctionType.Transpose:
                val = name.ToLower();
                break;

            case MathFunctionType.Power:
                val = "pow";
                break;

            case MathFunctionType.Sqsum:
                if (arguments == 1)
                {
                    return("sqsum_1");
                }
                else
                {
                    return("sqsum");
                }

            default:
                throw new NotImplementedException(String.Format("The math function `{0}' is not supported...", name));
            }

            return(val);
        }
コード例 #15
0
ファイル: Context.cs プロジェクト: codyn-net/rawc
        public override string MathFunctionV(Cdn.MathFunctionType type, Tree.Node node)
        {
            switch (type)
            {
            case MathFunctionType.Linsolve:
            case MathFunctionType.Inverse:
            case MathFunctionType.PseudoInverse:
            case MathFunctionType.Qr:
                if (((Formatters.C.Options)Options).NoLapack)
                {
                    throw new NotImplementedException(String.Format("The use of `{0}' is not supported without LAPACK at this moment",
                                                                    Enum.GetName(typeof(Cdn.MathFunctionType), type).ToLower()));
                }
                break;
            }

            switch (type)
            {
            case MathFunctionType.Linsolve:
            {
                var d2  = node.Children[1].Dimension;
                var ret = String.Format("CDN_MATH_LINSOLVE_V_{0}", d2.Rows);

                s_workspaces[ret] = new Workspace {
                    Type      = type,
                    Order     = new int[] { d2.Rows },
                    Dimension = d2,
                    WorkSize  = new int[] { d2.Rows },
                };

                return(ret);
            }

            case MathFunctionType.Inverse:
            {
                var d2  = node.Children[0].Dimension;
                var ret = String.Format("CDN_MATH_INVERSE_V_{0}", d2.Rows);

                if (!s_workspaces.ContainsKey(ret))
                {
                    int ws = Lapack.InverseWorkspace(d2.Rows);

                    s_workspaces[ret] = new Workspace {
                        Type      = type,
                        WorkSize  = new int[] { ws },
                        Dimension = d2,
                        Order     = new int[] { d2.Rows },
                    };
                }

                return(ret);
            }

            case MathFunctionType.PseudoInverse:
            {
                var d2  = node.Children[0].Dimension;
                var ret = Sparsify(String.Format("CDN_MATH_PSEUDOINVERSE_V_{0}_{1}", d2.Rows, d2.Columns), node);

                if (!s_workspaces.ContainsKey(ret))
                {
                    int[] ws = Lapack.PseudoInverseWorkspace(d2);

                    s_workspaces[ret] = new Workspace {
                        Type      = type,
                        Dimension = d2,
                        WorkSize  = ws,
                        Order     = new int[] { d2.Rows, d2.Columns },
                    };
                }

                return(ret);
            }

            case MathFunctionType.Qr:
            {
                var d2  = node.Children[0].Dimension;
                var ret = String.Format("CDN_MATH_QR_V_{0}_{1}", d2.Rows, d2.Columns);

                if (!s_workspaces.ContainsKey(ret))
                {
                    int ws = Lapack.QrWorkspace(d2);

                    s_workspaces[ret] = new Workspace {
                        Type      = type,
                        Dimension = d2,
                        WorkSize  = new int[] { ws },
                        Order     = new int[] { d2.Rows, d2.Columns },
                    };
                }

                return(ret);
            }

            case MathFunctionType.Slinsolve:
            {
                var d2  = node.Children[2].Dimension;
                var ret = String.Format("CDN_MATH_SLINSOLVE_V_{0}", d2.Rows);

                if (!s_workspaces.ContainsKey(ret))
                {
                    s_workspaces[ret] = new Workspace {
                        Type      = type,
                        Dimension = d2,
                        WorkSize  = new int[] { d2.Size() },
                        Order     = new int[] { d2.Rows }
                    };
                }

                return(ret);
            }

            case MathFunctionType.Multiply:
            {
                var d1 = node.Children[0].Dimension;
                var d2 = node.Children[1].Dimension;

                if (d1.Columns == d2.Rows && !(d1.IsOne || d2.IsOne) &&
                    d1.Rows <= 10 && d2.Columns <= 10 &&
                    !(node.Instruction is Instructions.SparseOperator))
                {
                    return(String.Format("CDN_MATH_{0}_NO_BLAS", base.MathFunctionV(type, node).ToUpper()));
                }

                break;
            }

            default:
                break;
            }

            return(String.Format("CDN_MATH_{0}", base.MathFunctionV(type, node).ToUpper()));
        }
コード例 #16
0
ファイル: Context.cs プロジェクト: codyn-net/rawc
 public override string MathFunction(Cdn.MathFunctionType type, int arguments)
 {
     return(String.Format("CDN_MATH_{0}", base.MathFunction(type, arguments).ToUpper()));
 }