Example #1
0
        /// <summary>
        /// Writes the matrix to a delimited file
        /// </summary>
        /// <param name="src">The source matrix</param>
        /// <param name="dst">The target file</param>
        /// <typeparam name="M">The natural row count type</typeparam>
        /// <typeparam name="N">The natural column count type</typeparam>
        /// <typeparam name="T">The element type</typeparam>
        public static void WriteTo <M, N, T>(BlockMatrix <M, N, T> src, FilePath dst, bool overwrite = true, TextFormat?fmt = null)
            where M : ITypeNat, new()
            where N : ITypeNat, new()
            where T : struct
        {
            var _fmt = fmt ?? TextFormat.Default;
            var sep  = _fmt.Delimiter;
            var rows = nati <M>();
            var cols = nati <N>();
            var sb   = new StringBuilder();

            sb.AppendLine($"{_fmt.CommentPrefix} {typeof(T).Name}[{rows}x{cols}]");
            if (_fmt.HasHeader)
            {
                for (var i = 0; i < cols; i++)
                {
                    sb.Append($"Col{i}");
                    if (i != cols - 1)
                    {
                        sb.Append(sep);
                    }
                }
                sb.AppendLine();
            }
            sb.Append(src.Format(sep));

            if (overwrite)
            {
                dst.Overwrite(sb.ToString());
            }
            else
            {
                dst.Append(sb.ToString());
            }
        }
Example #2
0
 public static string FormatTable <N, T>(BlockMatrix <N, T> src)
     where T : struct
     where N : ITypeNat, new()
 => src.Format(render : x => BitString.FromScalar(x).Format());