コード例 #1
0
ファイル: NDarray.cs プロジェクト: aidevnn/DesertLand
        public override string ToString()
        {
            var nargs   = new int[Shape.Length];
            var strides = Utils.Shape2Strides(Shape);

            Counters.Reset();

            string result = "";
            var    last = strides.Length == 1 ? Count : strides[strides.Length - 2];
            string before, after;

            List <Type> listValues = GetData.ToList();

            if (Utils.IsDebugLvl1)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine($"Class:{GetType().Name,-20}");
                if (Shape.Length > 1 || Shape[0] != 1)
                {
                    sb.AppendLine($"Shape:({Shape.Glue()}) Version:{GetHashCode(),10}");
                    sb.AppendLine($"Strides:({Strides.Glue()})");
                    sb.AppendLine($"OwnData:({OwnData})");
                }

                string dbg = $" : np.array([{listValues.Glue(",")}], dtype={OpsT.dtype}).reshape({Shape.Glue(",")})";
                var    nd  = $"NDArray<{typeof(Type).Name}>";
                sb.AppendLine($"{nd,-20} {Shape.Glue("x")}{dbg}");
                sb.AppendLine($"Counters. DataAccess:{Counters.Data} / MethCall:{Counters.MethCall}");
                Console.WriteLine(sb);
            }

            var    ml0 = listValues.Select(v => $"{v}").Max(v => v.Length);
            var    ml1 = listValues.Select(v => $"{v:F8}").Max(v => v.Length);
            string fmt = $"{{0,{ml0 + 2}}}";

            if (ml0 > ml1 + 3)
            {
                fmt = $"{{0,{ml1 + 2}:F8}}";
            }

            for (int idx = 0; idx < Count; ++idx)
            {
                after = before = "";

                if (idx % last == 0 || idx % last == last - 1)
                {
                    before = idx != 0 ? " " : "[";
                    after  = idx == Count - 1 ? "]" : "";
                    for (int l = strides.Length - 2; l >= 0; --l)
                    {
                        if (idx % strides[l] == 0)
                        {
                            before += "[";
                        }
                        else
                        {
                            before = " " + before;
                        }

                        if (idx % strides[l] == strides[l] - 1)
                        {
                            after += "]";
                        }
                    }
                }

                result += idx % last == 0 ? before : "";
                var val = listValues[idx];
                result += string.Format(fmt, val);
                result += idx % last == last - 1 ? after + "\n" : "";
                result += after.Length > 1 && idx != Count - 1 ? "\n" : "";
            }

            if (Utils.IsDebugNo)
            {
                result = result.Substring(0, result.Length - 1);
            }

            return(result);
        }