Esempio n. 1
0
        public Matrix(IList <Vector> vectors, VectorStyles vectorStyle)
        {
            if (vectors.Any())
            {
                if (vectorStyle == VectorStyles.Column)
                {
                    var rows    = vectors[0].Dimensions;
                    var columns = vectors.Count;

                    Rows    = rows;
                    Columns = columns;

                    _matrix = new double[rows][];
                    for (var i = 0; i < rows; i++)
                    {
                        _matrix[i] = new double[columns];
                    }

                    for (var i = 0; i < rows; i++)
                    {
                        for (var j = 0; j < columns; j++)
                        {
                            _matrix[i][j] = vectors[j].GetValueOfDimension(i);
                        }
                    }
                }
                else
                {
                    var rows    = vectors.Count;
                    var columns = vectors[0].Dimensions;

                    Columns = columns;
                    Rows    = rows;

                    _matrix = new double[rows][];
                    for (var i = 0; i < rows; i++)
                    {
                        _matrix[i] = new double[columns];
                    }

                    for (var i = 0; i < rows; i++)
                    {
                        for (var j = 0; j < columns; j++)
                        {
                            _matrix[i][j] = vectors[i].GetValueOfDimension(j);
                        }
                    }
                }
            }
            else
            {
                throw new Exception("No vectors to make a matrix!");
            }
        }
Esempio n. 2
0
        public static Vector4d Parse(string s,
                                     IFormatProvider provider = null,
                                     VectorStyles vstyle      = VectorStyles.All,
                                     NumberStyles style       = NumberStyles.Float | NumberStyles.AllowThousands)
        {
            Vector4d result;

            if (!TryParse(s, out result, provider, vstyle, style))
            {
                throw new Exception();
            }
            return(result);
        }
Esempio n. 3
0
        public static bool TryParse(string s,
                                    out Vector4d result,
                                    IFormatProvider provider = null,
                                    VectorStyles vstyle      = VectorStyles.All,
                                    NumberStyles style       = NumberStyles.Float | NumberStyles.AllowThousands)
        {
            Contract.Requires(s != null);

            double[] ret;
            if (!VectorUtils.TryParse(s, 4, out ret, double.TryParse, provider, vstyle, style))
            {
                result = Zero;
                return(false);
            }
            result = new Vector4d(ret[0], ret[1], ret[2], ret[3]);
            return(true);
        }
Esempio n. 4
0
        public static bool TryParse(string s,
                                    out Point2i result,
                                    IFormatProvider provider = null,
                                    VectorStyles vstyle      = VectorStyles.All,
                                    NumberStyles style       = NumberStyles.Float | NumberStyles.AllowThousands)
        {
            Contract.Requires(s != null);

            int[] ret;
            if (!VectorUtils.TryParse(s, 2, out ret, int.TryParse, provider, vstyle, style))
            {
                result = Point2i.Zero;
                return(false);
            }
            result = new Point2i(ret[0], ret[1]);
            return(true);
        }
Esempio n. 5
0
        /// <summary>
        ///     Intenta parsear la cadena de texto segun los estilos indicados y devuelve un array de valores.
        /// </summary>
        /// <param name="provider">Proveedor de formato.</param>
        /// <param name="s">Cadena de texto a parsear.</param>
        /// <param name="count">Numero de elementos tienen que leer. Si es -1, se leen todos.</param>
        /// <param name="vstyle">Estilo de vectores.</param>
        /// <param name="style">Estilo de numeros.</param>
        /// <param name="tryParse">Funcion de parseo.</param>
        /// <param name="result">Array de flotantes.</param>
        /// <returns>Indica si lo ha parseado correctamente.</returns>
        public static bool TryParse <T>(string s, int count,
                                        out T[] result,
                                        TryParse <T> tryParse,
                                        IFormatProvider provider = null,
                                        VectorStyles vstyle      = VectorStyles.All,
                                        NumberStyles style       = NumberStyles.Float | NumberStyles.AllowThousands)
        {
            try
            {
                // Se obtiene la configuracion.
                VectorFormatInfo vfi = null;
                if (provider != null)
                {
                    vfi = provider.GetFormat <VectorFormatInfo>();
                }
                if (vfi == null)
                {
                    vfi = VectorFormatInfo.CurrentInfo;
                }

                bool usarPrincipioYFin = ((vstyle ^ VectorStyles.BegEnd) != 0) && vfi.HasBegEnd;
                bool usarSeparador     = ((vstyle ^ VectorStyles.Sep) != 0) && vfi.HasSep;

                // Se parsea la cadena utilizando expresiones regulares.
                const RegexOptions opciones = RegexOptions.ExplicitCapture
                                              | RegexOptions.IgnoreCase
                                              | RegexOptions.IgnorePatternWhitespace
                                              | RegexOptions.Multiline
                                              | RegexOptions.Compiled;

                string s2;
                if (usarPrincipioYFin)
                {
                    // Se busca el inicio y fin.
                    string patron1 = string.Format(provider,
                                                   @"^\s*(?:{0}\s*)?(?<interior>[^{0}{1}]*)\s*(?:{1}\s*)?$",
                                                   Regex.Escape(vfi.Beg),
                                                   Regex.Escape(vfi.End));
                    Regex rx1 = new Regex(patron1, opciones);
                    Match m1  = rx1.Match(s);
                    if (!m1.Success)
                    {
                        result = null;
                        return(false);
                    }
                    s2 = m1.Groups["interior"].Value;
                }
                else
                {
                    // Se busca el inicio y fin.
                    string patron1 = string.Format(provider, @"^\s*(?<interior>.*)\s*$");
                    Regex  rx1     = new Regex(patron1, opciones);
                    Match  m1      = rx1.Match(s);
                    if (!m1.Success)
                    {
                        result = null;
                        return(false);
                    }
                    s2 = m1.Groups["interior"].Value;
                }

                string[] ss;
                if (usarSeparador)
                {
                    // Se buscan los separadores.
                    string patron2 = string.Format(provider, @"\s*{0}\s*",
                                                   Regex.Escape(vfi.Sep));
                    Regex rx2 = new Regex(patron2, opciones);
                    ss = rx2.Split(s2);
                    if (ss.Length != 2)
                    {
                        result = null;
                        return(false);
                    }
                }
                else
                {
                    // Se buscan los separadores.
                    string patron2 = string.Format(provider, @"\s+");
                    Regex  rx2     = new Regex(patron2, opciones);
                    ss = rx2.Split(s2);
                    if (ss.Length != 2)
                    {
                        result = null;
                        return(false);
                    }
                }

                if (count > 0 && ss.Length != count)
                {
                    result = null;
                    return(false);
                }

                T[] ret = new T[ss.Length];
                for (int i = 0; i < ss.Length; i++)
                {
                    if (!tryParse(ss[i], style, provider, out ret[i]))
                    {
                        result = null;
                        return(false);
                    }
                }
                result = ret;
                return(true);
            }
            catch (Exception)
            {
                result = null;
                return(false);
            }
        }