有没有办法从教堂的数组中打印格式化的 table?
Is there a way to print a formatted table from an array in chapel?
我正在研究小教堂的传热问题,虽然我知道我无法获得可以使用的漂亮 GUI,但我想要一些可以打印的东西我至少可以看到 运行 我的代码的最终结果。到目前为止,我所拥有的内容很难阅读,因为较长的数字会挤出整行。我正在考虑使用 formattedIO,但文档让我感到困惑,并且似乎更多地关注一行格式而不是任何 table 相关的内容。我所希望的是确保所有列都对齐的某种方法。如果有帮助,这是我的代码。提前致谢!
use Math;
use Time;
var rows = 10;
var cols = 10;
var times = 4;
var x = 5;
var y = 5;
var temps: [0..rows+1, 0..cols+1] real = 0;
var past: [0..rows+1, 0..cols+1] real;
temps[x,y] = 100;
past[x,y] = 100;
var t: Timer;
t.start();
for t in 1..times do {
forall i in 1..rows do {
for j in 1..cols do {
temps[i,j] = floor((past[i-1,j]+past[i+1,j]+past[i,j-1]+past[i,j+1])/4);
//floor was used to cut off extra decimals in an attempt to make the display better
}
}
past = temps;
}
t.stop();
writeln(t.elapsed());
writeln(temps);
通常网格要大很多 (1000 x 1000),但我把它做得更小以便我可以看到这些值。我真的很想找到一种方法来制作它,以便可以以不糟糕的方式打印更大的网格。是否有必要为更大的网格尺寸使用文件输出?
如果有一个用于打印格式化 tables 的库会很棒,但我还不知道有一个可用的库。
现在您可以计算每个 table 单元格的宽度,然后使用格式字符串将填充指定为最大宽度。这是一个例子:
use IO;
var data:[1..5, 1..5] real;
// Compute some numbers that need formatting help
for (ij,elt) in zip(data.domain, data) {
var (i, j) = ij;
if i == j {
elt = 0.0;
} else {
var d = i-j;
elt = d*d*d*17.20681 - j*0.1257201;
}
}
// %r tries to use e.g. 12.25 and exponential for big/small
// here "r" is for "real"
// %dr ("decimal real") means always like 12.25
// %er ("exponential real") is always like 1.2e27
// and %{####.####} is available to show the pattern you want
// but will never output in exponential
// Note that you can specify padding:
// %17r would be padded on the left with spaces to 17 columns
// %017r would be padded on the left with zeros to 17 columns
// %-17r would be padded on the right with spaces to 17 columns
// You can also use %*r to specify the padding in an argument
// Let's compute the maximum number of characters for each element
// This uses the string.format function
// https://chapel-lang.org/docs/master/modules/standard/IO/FormattedIO.html#FormattedIO.string.format
// as well as promotion.
// compute the maximum size of any field
// (this could be adapted to be per-column, say)
var width = 0;
for i in data.domain.dim(0) {
for j in data.domain.dim(1) {
var mywidth = "%r".format(data[i,j]).size;
if width < mywidth then
width = mywidth;
}
}
// Now format all of the elements with that size
for i in data.domain.dim(0) {
var first = true;
for j in data.domain.dim(1) {
if first==false then
write(" ");
writef("%*r", width, data[i,j]);
first = false;
}
writeln();
}
我正在研究小教堂的传热问题,虽然我知道我无法获得可以使用的漂亮 GUI,但我想要一些可以打印的东西我至少可以看到 运行 我的代码的最终结果。到目前为止,我所拥有的内容很难阅读,因为较长的数字会挤出整行。我正在考虑使用 formattedIO,但文档让我感到困惑,并且似乎更多地关注一行格式而不是任何 table 相关的内容。我所希望的是确保所有列都对齐的某种方法。如果有帮助,这是我的代码。提前致谢!
use Math;
use Time;
var rows = 10;
var cols = 10;
var times = 4;
var x = 5;
var y = 5;
var temps: [0..rows+1, 0..cols+1] real = 0;
var past: [0..rows+1, 0..cols+1] real;
temps[x,y] = 100;
past[x,y] = 100;
var t: Timer;
t.start();
for t in 1..times do {
forall i in 1..rows do {
for j in 1..cols do {
temps[i,j] = floor((past[i-1,j]+past[i+1,j]+past[i,j-1]+past[i,j+1])/4);
//floor was used to cut off extra decimals in an attempt to make the display better
}
}
past = temps;
}
t.stop();
writeln(t.elapsed());
writeln(temps);
通常网格要大很多 (1000 x 1000),但我把它做得更小以便我可以看到这些值。我真的很想找到一种方法来制作它,以便可以以不糟糕的方式打印更大的网格。是否有必要为更大的网格尺寸使用文件输出?
如果有一个用于打印格式化 tables 的库会很棒,但我还不知道有一个可用的库。
现在您可以计算每个 table 单元格的宽度,然后使用格式字符串将填充指定为最大宽度。这是一个例子:
use IO;
var data:[1..5, 1..5] real;
// Compute some numbers that need formatting help
for (ij,elt) in zip(data.domain, data) {
var (i, j) = ij;
if i == j {
elt = 0.0;
} else {
var d = i-j;
elt = d*d*d*17.20681 - j*0.1257201;
}
}
// %r tries to use e.g. 12.25 and exponential for big/small
// here "r" is for "real"
// %dr ("decimal real") means always like 12.25
// %er ("exponential real") is always like 1.2e27
// and %{####.####} is available to show the pattern you want
// but will never output in exponential
// Note that you can specify padding:
// %17r would be padded on the left with spaces to 17 columns
// %017r would be padded on the left with zeros to 17 columns
// %-17r would be padded on the right with spaces to 17 columns
// You can also use %*r to specify the padding in an argument
// Let's compute the maximum number of characters for each element
// This uses the string.format function
// https://chapel-lang.org/docs/master/modules/standard/IO/FormattedIO.html#FormattedIO.string.format
// as well as promotion.
// compute the maximum size of any field
// (this could be adapted to be per-column, say)
var width = 0;
for i in data.domain.dim(0) {
for j in data.domain.dim(1) {
var mywidth = "%r".format(data[i,j]).size;
if width < mywidth then
width = mywidth;
}
}
// Now format all of the elements with that size
for i in data.domain.dim(0) {
var first = true;
for j in data.domain.dim(1) {
if first==false then
write(" ");
writef("%*r", width, data[i,j]);
first = false;
}
writeln();
}