当前位置: 首页>>代码示例>>C#>>正文


C# Variable.GetShape方法代码示例

本文整理汇总了C#中Variable.GetShape方法的典型用法代码示例。如果您正苦于以下问题:C# Variable.GetShape方法的具体用法?C# Variable.GetShape怎么用?C# Variable.GetShape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Variable的用法示例。


在下文中一共展示了Variable.GetShape方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SplitSecondDimension

        public static Point[][] SplitSecondDimension(Variable variable)
        {
            if (variable == null)
                throw new ArgumentNullException("variable");
            if (variable.Rank != 2)
                throw new ArgumentException("Only 2d variables are supported");
            int n = variable.GetShape()[1];
            int m = variable.GetShape()[0];
            Array d = variable.GetData();
            Point[][] pts = new Point[n][];

            for (int i = 0; i < n; i++)
            {
                List<Point> tmp = new List<Point>(m);
                if (variable.TypeOfData == typeof(float))
                {
                    float[,] dt = (float[,])d;
                    object mvObj = variable.GetMissingValue();
                    if (mvObj == null)
                    {
                        for (int j = 0; j < m; j++)
                            if (!Double.IsNaN(dt[j, i]))
                                tmp.Add(new Point(j, dt[j, i]));
                    }
                    else
                    {
                        float mv;
                        try
                        {
                            mv = Convert.ToSingle(mvObj);
                        }
                        catch (Exception exc)
                        {
                            Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to float: " + exc.Message);
                            mv = float.NaN;
                        }
                        for (int j = 0; j < m; j++)
                            if (dt[j, i] != mv)
                                tmp.Add(new Point(j, dt[j, i]));
                    }
                }
                else if (variable.TypeOfData == typeof(double))
                {
                    double[,] dt = (double[,])d;

                    object mvObj = variable.GetMissingValue();
                    if (mvObj == null)
                    {
                        for (int j = 0; j < m; j++)
                            if (!Double.IsNaN(dt[j, i]))
                                tmp.Add(new Point(j, dt[j, i]));
                    }
                    else
                    {
                        double mv;
                        try
                        {
                            mv = Convert.ToDouble(mvObj);
                        }
                        catch (Exception exc)
                        {
                            Trace.WriteLine("PolyPolyline: cannot convert missing value attribute to double: " + exc.Message);
                            mv = double.NaN;
                        }
                        for (int j = 0; j < m; j++)
                            if (dt[j, i] != mv)
                                tmp.Add(new Point(j, dt[j, i]));
                    }
                }
                else
                {
                    object mv = variable.GetMissingValue();
                    for (int j = 0; j < m; j++)
                    {
                        object obj = d.GetValue(j, i);
                        if (!Object.Equals(obj, mv))
                            tmp.Add(new Point(j, Convert.ToDouble(obj)));
                    }
                }
                pts[i] = tmp.ToArray();
            }
            return pts;
        }
开发者ID:modulexcite,项目名称:SDSlite,代码行数:83,代码来源:Split2d.cs

示例2: PrintData

        static void PrintData(Variable v, string range, string format)
        {
            try
            {
                // Find out which data to take
                int rank = v.Rank;
                var shape = v.GetShape();
                var origin = rank == 0 ? null : new int[rank];
                var count = rank == 0 ? null : new int[rank];
                var stride = rank == 0 ? null : new int[rank];
                for (int i = 0; i < rank; i++)
                {
                    origin[i] = 0;
                    count[i] = shape[i];
                    stride[i] = 1;
                }
                if (range != null)
                {
                    // parse range specification 'from:to' or 'from:step:to'
                    var ranges = range.Split(',');
                    for (int i = 0; i < rank && i < ranges.Length; i++)
                    {
                        var fromto = ranges[i].Split(':');
                        if (!int.TryParse(fromto[0], out origin[i])) origin[i] = 0;
                        if (origin[i] < 0) origin[i] = 0;
                        if (origin[i] >= shape[i]) origin[i] = shape[i] - 1;
                        if (fromto.Length < 2) count[i] = 1;
                        else
                        {
                            if (!int.TryParse(fromto[fromto.Length - 1], out count[i])) count[i] = 0;
                            if (count[i] == 0 || count[i] >= shape[i]) count[i] = shape[i] - 1;
                            if (count[i] < origin[i]) count[i] = origin[i];
                            if (fromto.Length > 2)
                            {
                                if (!int.TryParse(fromto[1], out stride[i])) stride[i] = 1;
                                if (stride[i] <= 0) stride[i] = 1;
                            }
                            count[i] = 1 + (count[i] - origin[i]) / stride[i];
                        }
                    }
                }
                // Now get the data
                Array data = v.GetData(origin, stride, count);
                if (data == null || (rank > 0 && data.Rank != rank))
                    Console.Error.WriteLine("No data");
                else
                {
                    int prefix = string.Join(",", Array.ConvertAll(shape, i => i.ToString())).Length + 2;
                    // parse format
                    int cell = 8;
                    string frm = "{0,8}";
                    if (format != null)
                    {
                        int p = format.IndexOf(':');
                        if (p < 0)
                        {
                            if (!int.TryParse(format, out cell)) cell = 8;
                            frm = "{0," + cell + "}";
                        }
                        else
                        {
                            if (!int.TryParse(format.Substring(0, p), out cell)) cell = 8;
                            frm = "{0," + cell + format.Substring(p) + "}";
                        }
                    }

                    // Now print the data
                    if (rank == 0) // scalar variable
                    {
                        Console.WriteLine();
                        string item = string.Format(frm, data.GetValue(0));
                        // check that item fits a cell
                        if (item.Length > cell) item = item.Substring(0, cell - 1) + "#";
                        Console.Write(" "); Console.Write(item);
                    }
                    else // has rank > 0 (array)
                    {
                        int[] indices = new int[data.Rank];
                        int[] varIndices = new int[rank];
						int bufferWidth = Math.Max(80, Console.BufferWidth);
						// .. to allow for non-console output
                        string lineSeparator = Environment.NewLine;
                        while (indices[0] < data.GetLength(0))
                        {
                            // print last dimension
                            Console.Write(lineSeparator);
                            lineSeparator = string.Empty;
                            int lastIndex = data.Rank - 1;
                            indices[lastIndex] = 0;
                            while (indices[lastIndex] < data.GetLength(lastIndex))
                            {
                                // print line of cells
                                // print prefix of current indices
                                for (int i = 0; i < rank; i++)
                                    varIndices[i] = origin[i] + indices[i] * stride[i];
                                string item = ("[" + string.Join(",", Array.ConvertAll(varIndices, i => i.ToString())) + "]").PadRight(prefix);
                                Console.Write(item);
                                int left = bufferWidth - item.Length;
                                while (left > cell && indices[lastIndex] < data.GetLength(lastIndex))
                                {
//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:SDSlite,代码行数:101,代码来源:Program.cs


注:本文中的Variable.GetShape方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。