maxima: 打印一个漂亮的表格,类似 printf

huangapple go评论45阅读模式
英文:

maxima : printing a nice table ala printf

问题

Sure, here's the translation of your request:

maxima初学者问题:如何打印一个漂亮的固定格式表格,比如(R代码):

> for (i in 1:5) printf("%4d\t%7.3f\t%s\t%12.5f\n", i, sqrt(i), paste0("C",i), exp(i))
   1	  1.000	C1	     2.71828
   2	  1.414	C2	     7.38906
   3	  1.732	C3	    20.08554
   4	  2.000	C4	    54.59815
   5	  2.236	C5	   148.41316

希望这有所帮助。

英文:

maxima beginner question: how do I print a nice fixed format table, such as (R code):

> for (i in 1:5) printf("%4d\t%7.3f\t%s\t%12.5f\n", i, sqrt(i), paste0("C",i), exp(i))
   1	  1.000	C1	     2.71828
   2	  1.414	C2	     7.38906
   3	  1.732	C3	    20.08554
   4	  2.000	C4	    54.59815
   5	  2.236	C5	   148.41316

advice appreciated.

答案1

得分: 1

在Maxima中有一个printf函数,它通过调用Common Lisp实现中的FORMAT函数来生成格式化输出。在网络搜索中查找"Common Lisp Hyperspec FORMAT"将找到该规范。

FORMAT,以及因此printf,使用波浪号而不是百分号标记格式说明符。格式说明符的参数与C或其他语言中的参数类似但不同;请参阅CLHS获取详细信息。

在Maxima中有一些CLHS中没有的格式说明符。~m以漂亮的打印机的形式显示一个Maxima表达式,~h显示一个bigfloat,如果我记得正确的话。

你展示的格式字符串可能等价于以下内容(我没有测试它):

for i: 1 thru 5
    do printf (true, "~4d~0,8t~7,3f~0,8t~a~0,8t~12,5f~%", i, float (sqrt (i)), sconcat ("C", i), float (exp (i)));

但很多时候我只是把东西放在一个矩阵中,然后Maxima的漂亮打印机会在没有我进一步努力的情况下很好地对齐。

英文:

There is a printf function in Maxima, which produces formatted output, mostly by punting to the function FORMAT in the Common Lisp implementation. A web search for "Common Lisp Hyperspec FORMAT" will find the spec for that.

FORMAT, and therefore printf, marks format specifiers with tilde instead of percent sign. The arguments for the format specifiers are similar but different than in C or other languages; see the CLHS for details.

There are a few specifiers for Maxima which aren't in the CLHS. ~m displays a Maxima expression as in the pretty printer, and ~h displays a bigfloat, if I remember correctly.

The format string you showed might be equivalent to this (I didn't test it):

for i: 1 thru 5
    do printf (true, "~4d~0,8t~7,3f~0,8t~a~0,8t~12,5f~%", i, float (sqrt (i)), sconcat ("C", i), float (exp (i)));

But many times I just put stuff in a matrix and then the Maxima pretty printer aligns stuff pretty well without any further effort on my part.

huangapple
  • 本文由 发表于 2023年4月11日 12:24:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定