Beispiel #1
0
        public static SVMProblem Convert(svm_problem x)
        {
            double[] y_array = new double[x.l];
            Marshal.Copy(x.y, y_array, 0, y_array.Length);

            List<SVMNode[]> x_array = new List<SVMNode[]>();
            IntPtr i_ptr_x = x.x;
            for (int i = 0; i < x.l; i++)
            {
                IntPtr ptr_nodes = (IntPtr)Marshal.PtrToStructure(i_ptr_x, typeof(IntPtr));
                SVMNode[] nodes = SVMNode.Convert(ptr_nodes);
                x_array.Add(nodes);
                i_ptr_x = IntPtr.Add(i_ptr_x, Marshal.SizeOf(typeof(IntPtr)));
            }

            SVMProblem y = new SVMProblem();
            for (int i = 0; i < x.l; i++)
            {
                y.Add(x_array[i], y_array[i]);
            }

            return y;
        }
Beispiel #2
0
        public static IntPtr Allocate(SVMProblem x)
        {
            if (x == null || x.X == null || x.Y == null || x.Length < 1)
            {
                return IntPtr.Zero;
            }

            svm_problem y = new svm_problem();
            y.l = x.Length;

            // Allocate problem.y
            y.y = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.Y.Count);
            Marshal.Copy(x.Y.ToArray(), 0, y.y, x.Y.Count);

            // Allocate problem.x
            y.x = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * x.X.Count);
            IntPtr i_ptr_x = y.x;
            for (int i = 0; i < x.X.Count; i++)
            {
                // Prepare each node array 
                // 1) All nodes containing zero value is removed 
                // 2) A node which index is -1 is added to the end
                List<SVMNode> temp = x.X[i].Where(a => a.Value != 0).ToList();
                temp.Add(new SVMNode(-1, 0));
                SVMNode[] nodes = temp.ToArray();

                // Allocate node array
                IntPtr ptr_nodes = SVMNode.Allocate(nodes);
                Marshal.StructureToPtr(ptr_nodes, i_ptr_x, true);

                i_ptr_x = IntPtr.Add(i_ptr_x, Marshal.SizeOf(typeof(IntPtr)));
            }

            // Allocate the problem
            int size = Marshal.SizeOf(y);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(y, ptr, true);

            return ptr;
        }
Beispiel #3
0
        public static void Free(svm_problem x)
        {
            Marshal.FreeHGlobal(x.y);
            x.y = IntPtr.Zero;

            IntPtr i_ptr_x = x.x;
            for (int i = 0; i < x.l; i++)
            {
                IntPtr ptr_nodes = (IntPtr)Marshal.PtrToStructure(i_ptr_x, typeof(IntPtr));
                SVMNode.Free(ptr_nodes);

                i_ptr_x = IntPtr.Add(i_ptr_x, Marshal.SizeOf(typeof(IntPtr)));
            }

            Marshal.FreeHGlobal(x.x);
            x.x = IntPtr.Zero;
        }