Esempio n. 1
0
        public Matrix(MatrixTemplateSizeParameter parameter)
            : base(parameter.TemplateRows, parameter.TemplateColumns)
        {
            if (!TryParse(typeof(TElement), out var type))
                throw new NotSupportedException($"{typeof(TElement).Name} does not support");
            if (parameter == null)
                throw new ArgumentNullException(nameof(parameter));
            if (parameter.TemplateRows < 0)
                throw new ArgumentOutOfRangeException($"{nameof(parameter.TemplateRows)}", $"{nameof(parameter.TemplateRows)} should be positive value.");
            if (parameter.TemplateColumns < 0)
                throw new ArgumentOutOfRangeException($"{nameof(parameter.TemplateColumns)}", $"{nameof(parameter.TemplateColumns)} should be positive value.");

            var templateRows = parameter.TemplateRows;
            var templateColumns = parameter.TemplateColumns;

            var error = NativeMethods.matrix_new4(type.ToNativeMatrixElementType(),
                                                  (uint)templateRows,
                                                  (uint)templateColumns,
                                                  out var ret);
            switch (error)
            {
                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"Input {type} is not supported.");
                case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport:
                    throw new ArgumentException($"{nameof(templateRows)} or {nameof(templateRows)} is not supported.");
            }

            this.NativePtr = ret;
            this._MatrixElementTypes = type;
            this._ElementType = type.ToNativeMatrixElementType();
            this._Indexer = this.CreateIndexer(type);
        }
Esempio n. 2
0
        public static uint NearestCenter <T>(IEnumerable <Matrix <T> > centers, Matrix <T> sample)
            where T : struct
        {
            if (centers == null)
            {
                throw new ArgumentNullException(nameof(centers));
            }
            if (sample == null)
            {
                throw new ArgumentNullException(nameof(sample));
            }

            var centerArray = centers.ToArray();

            if (!centerArray.Any())
            {
                throw new ArgumentException($"{nameof(centers)} does not contain any element");
            }

            var center = centerArray.FirstOrDefault();

            if (center == null)
            {
                throw new ArgumentException($"{nameof(centers)} contains null object", nameof(centers));
            }

            var templateRow    = sample.TemplateRows;
            var templateColumn = sample.TemplateColumns;

            var param = new MatrixTemplateSizeParameter(templateRow, templateColumn);

            using (var inCenters = new StdVector <Matrix <T> >(centerArray, param))
            {
                var type = sample.MatrixElementType.ToNativeMatrixElementType();
                var ret  = NativeMethods.nearest_center(type,
                                                        templateRow,
                                                        templateColumn,
                                                        inCenters.NativePtr,
                                                        sample.NativePtr,
                                                        out var result);
                switch (ret)
                {
                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"{type} is not supported.");

                case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport:
                    throw new ArgumentException($"{nameof(sample.TemplateColumns)} or {nameof(sample.TemplateRows)} is not supported.");
                }

                return(result);
            }
        }
Esempio n. 3
0
        public Matrix(MatrixTemplateSizeParameter parameter, TElement[] array)
            : base(parameter.TemplateRows, parameter.TemplateColumns)
        {
            if (!TryParse(typeof(TElement), out var type))
            {
                throw new NotSupportedException($"{typeof(TElement).Name} does not support");
            }
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }
            if (parameter.TemplateRows < 0)
            {
                throw new ArgumentOutOfRangeException($"{nameof(parameter.TemplateRows)}", $"{nameof(parameter.TemplateRows)} should be positive value.");
            }
            if (parameter.TemplateColumns < 0)
            {
                throw new ArgumentOutOfRangeException($"{nameof(parameter.TemplateColumns)}", $"{nameof(parameter.TemplateColumns)} should be positive value.");
            }

            var templateRows    = parameter.TemplateRows;
            var templateColumns = parameter.TemplateColumns;

            if (templateRows * templateColumns != 0)
            {
                if (templateRows * templateColumns != array.Length)
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
            else if (templateRows != 0)
            {
                if (array.Length % templateRows != 0)
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
            else if (templateColumns != 0)
            {
                if (array.Length % templateColumns != 0)
                {
                    throw new ArgumentOutOfRangeException();
                }
            }

            using (var vector = new StdVector <TElement>(array))
            {
                var error = NativeMethods.matrix_new5(type.ToNativeMatrixElementType(),
                                                      (uint)templateRows,
                                                      (uint)templateColumns,
                                                      vector.NativePtr,
                                                      out var ret);
                switch (error)
                {
                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"Input {type} is not supported.");

                case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport:
                    throw new ArgumentException($"{nameof(templateRows)} or {nameof(templateRows)} is not supported.");
                }

                this.NativePtr           = ret;
                this._MatrixElementTypes = type;
                this._ElementType        = type.ToNativeMatrixElementType();
                this._Indexer            = this.CreateIndexer(type);
            }
        }
Esempio n. 4
0
        public static Matrix <TScalar> RankFeatures <TScalar, TKernel>(KCentroid <TScalar, TKernel> kcentroid,
                                                                       IEnumerable <Matrix <TScalar> > samples,
                                                                       IEnumerable <TScalar> labels)
            where TScalar : struct
            where TKernel : KernelBase
        {
            if (kcentroid == null)
            {
                throw new ArgumentNullException(nameof(kcentroid));
            }
            if (samples == null)
            {
                throw new ArgumentNullException(nameof(samples));
            }
            if (labels == null)
            {
                throw new ArgumentNullException(nameof(labels));
            }

            var sampleArray = samples.ToArray();
            var labelsArray = labels.ToArray();

            var first = sampleArray.FirstOrDefault();

            if (first == null)
            {
                throw new ArgumentException($"{nameof(samples)} contains null object", nameof(samples));
            }

            var templateRow    = first.TemplateRows;
            var templateColumn = first.TemplateColumns;

            foreach (var sample in sampleArray)
            {
                if (sample == null)
                {
                    throw new ArgumentException($"{nameof(samples)} contains null object", nameof(samples));
                }
                if (sample.TemplateRows != templateRow)
                {
                    throw new ArgumentException($"{nameof(samples)} contains different {nameof(sample.TemplateRows)} of {typeof(Matrix<TScalar>).Name}", nameof(samples));
                }
                if (sample.TemplateColumns != templateColumn)
                {
                    throw new ArgumentException($"{nameof(samples)} contains different {nameof(sample.TemplateColumns)} of {typeof(Matrix<TScalar>).Name}", nameof(samples));
                }
            }

            var param = new MatrixTemplateSizeParameter(templateRow, templateColumn);

            using (var inSamples = new StdVector <Matrix <TScalar> >(sampleArray, param))
                using (var inLabels = new StdVector <TScalar>(labelsArray))
                {
                    var type  = first.MatrixElementType.ToNativeMatrixElementType();
                    var error = NativeMethods.rank_features(kcentroid.Parameter.KernelType.ToNativeKernelType(),
                                                            type,
                                                            templateRow,
                                                            templateColumn,
                                                            kcentroid.NativePtr,
                                                            inSamples.NativePtr,
                                                            inLabels.NativePtr,
                                                            out var ret);
                    switch (error)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new ArgumentException($"{type} is not supported.");

                    case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport:
                        throw new ArgumentException($"{nameof(first.TemplateColumns)} or {nameof(first.TemplateRows)} is not supported.");

                    case NativeMethods.ErrorType.SvmKernelNotSupport:
                        throw new ArgumentException($"{kcentroid.Parameter.KernelType} is not supported.");
                    }

                    return(new Matrix <TScalar>(ret, 0, 2));
                }
        }
Esempio n. 5
0
        public static IEnumerable <Matrix <T> > FindClustersUsingAngularKMeans <T>(IEnumerable <Matrix <T> > samples, IEnumerable <Matrix <T> > centers, uint maxIteration = 1000)
            where T : struct
        {
            if (samples == null)
            {
                throw new ArgumentNullException(nameof(samples));
            }
            if (centers == null)
            {
                throw new ArgumentNullException(nameof(centers));
            }

            var sampleArray = samples.ToArray();

            if (!sampleArray.Any())
            {
                yield break;
            }

            var centerArray = centers.ToArray();

            if (!centerArray.Any())
            {
                yield break;
            }

            var sample = sampleArray.FirstOrDefault();

            if (sample == null)
            {
                throw new ArgumentException($"{nameof(samples)} contains null object", nameof(samples));
            }

            var center = centerArray.FirstOrDefault();

            if (center == null)
            {
                throw new ArgumentException($"{nameof(centers)} contains null object", nameof(centers));
            }

            var templateRow    = sample.TemplateRows;
            var templateColumn = sample.TemplateColumns;

            var param = new MatrixTemplateSizeParameter(templateRow, templateColumn);

            using (var inSamples = new StdVector <Matrix <T> >(sampleArray, param))
                using (var inCenters = new StdVector <Matrix <T> >(centerArray, param))
                    using (var outResult = new StdVector <Matrix <T> >(param))
                    {
                        var type = sample.MatrixElementType.ToNativeMatrixElementType();
                        var ret  = NativeMethods.find_clusters_using_angular_kmeans(type,
                                                                                    templateRow,
                                                                                    templateColumn,
                                                                                    inCenters.NativePtr,
                                                                                    inSamples.NativePtr,
                                                                                    maxIteration,
                                                                                    outResult.NativePtr);
                        switch (ret)
                        {
                        case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                            throw new ArgumentException($"{type} is not supported.");

                        case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport:
                            throw new ArgumentException($"{nameof(sample.TemplateColumns)} or {nameof(sample.TemplateRows)} is not supported.");
                        }

                        foreach (var result in outResult.ToArray())
                        {
                            yield return(result);
                        }
                    }
        }
Esempio n. 6
0
        public static IEnumerable <Matrix <TScalar> > PickInitialCenters <TScalar, TKernel>(int numberCenters,
                                                                                            IEnumerable <Matrix <TScalar> > samples,
                                                                                            TKernel kernel,
                                                                                            double percentile = 0.01)
            where TScalar : struct
            where TKernel : KernelBase
        {
            if (samples == null)
            {
                throw new ArgumentNullException(nameof(samples));
            }

            var sampleArray = samples.ToArray();

            if (!sampleArray.Any())
            {
                return(new Matrix <TScalar> [0]);
            }

            var first = sampleArray.FirstOrDefault();

            if (first == null)
            {
                throw new ArgumentException($"{nameof(samples)} contains null object", nameof(samples));
            }

            var templateRow    = first.TemplateRows;
            var templateColumn = first.TemplateColumns;

            foreach (var sample in sampleArray)
            {
                if (sample == null)
                {
                    throw new ArgumentException($"{nameof(samples)} contains null object", nameof(samples));
                }
                if (sample.TemplateRows != templateRow)
                {
                    throw new ArgumentException($"{nameof(samples)} contains different {nameof(sample.TemplateRows)} of {typeof(Matrix<TScalar>).Name}", nameof(samples));
                }
                if (sample.TemplateColumns != templateColumn)
                {
                    throw new ArgumentException($"{nameof(samples)} contains different {nameof(sample.TemplateColumns)} of {typeof(Matrix<TScalar>).Name}", nameof(samples));
                }
            }

            var param = new MatrixTemplateSizeParameter(templateRow, templateColumn);

            using (var inSamples = new StdVector <Matrix <TScalar> >(sampleArray, param))
                using (var outCenters = new StdVector <Matrix <TScalar> >(0, param))
                {
                    var type = first.MatrixElementType.ToNativeMatrixElementType();
                    var ret  = NativeMethods.pick_initial_centers(kernel.KernelType.ToNativeKernelType(),
                                                                  type,
                                                                  templateRow,
                                                                  templateColumn,
                                                                  numberCenters,
                                                                  outCenters.NativePtr,
                                                                  inSamples.NativePtr,
                                                                  kernel.NativePtr,
                                                                  percentile);
                    switch (ret)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new ArgumentException($"{type} is not supported.");

                    case NativeMethods.ErrorType.MatrixElementTemplateSizeNotSupport:
                        throw new ArgumentException($"{nameof(first.TemplateColumns)} or {nameof(first.TemplateRows)} is not supported.");

                    case NativeMethods.ErrorType.SvmKernelNotSupport:
                        throw new ArgumentException($"{kernel.KernelType} is not supported.");
                    }

                    return(outCenters.ToArray());
                }
        }