-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need better array printing - especially for complex arrays #29
Comments
Actually, while we are at it, perhaps we should think of what features we should have in printing. Do we want to support a simple markup (maybe just basic html tags)? The markup can be then interpreted differently in different environments such as the console, an IDE, in a browser etc. That way, we can avoid hardcoding newlines and spaces into the printed output, and let the presentation layer figure it out. For example, if you resize the window, things can be made to look nicer automatically etc. |
That's a cool idea, but I'm not sure we should go there just yet. Maybe we can make a speculative feature issue for that one. |
Agree that this is for a future date. This was the first realization I had when I tried repl-cloud. -viral On Jun 3, 2011, at 7:36 PM, StefanKarpinski wrote:
|
Might be worthwhile to implement printf, and then implement pretty printing of arrays with printf. What's the way to do this? Implement printf in julia, or is it possible to just call the C printf? |
Well, I guess printf can be done easily: julia> ccall(:printf, Void, (Ptr{Uint8}, Int32, Int32), "%d, %d\n", 1, 2) |
This crashes though: julia> ccall(:printf, Void, (Ptr{Uint8}, Float64, Int32), "%5.2lf, %d\n", 1/3, 2) |
I'm guessing that it may have something to do with calling a varargs function. I'm not convinced this is the best way for us to do this, especially since printf is notoriously unsafe. That's acceptable in C because lots of things are unsafe, so printf is in equally dodgy company, but Julia code really ought to be much safer. |
Works on linux. But yes, there's probably something strange about passing arguments of different types to a varargs function. We should write a wrapper for printf that looks at the format string to check/convert arguments, then calls printf on each format specifier individually. |
I can take that on. Parsing strings is something I've done a lot of in Julia at this point. |
This is shockingly hard to implement. |
This is like 90% done as of d1af957. The only thing that remains is to implement new printing for tensors with N ≥ 3. Any thoughts on how that should work? |
This is so great. Bravo. A couple comments:
This is especially confusing in light of how our
julia> complex(rand(4,4),rand(4,4))
There's a misaligned decimal in there.
|
OK, another one: using this format for cell arrays makes certain data structures really ugly:
I think the former special case for 1-d arrays was justified. |
Thanks. I still can't believe how hard this was to get right. It seems so trivial.
Yeah, I can resurrect that code. I just deleted all the old code to put this in it's place. Ain't version control great?
What do you propose as a better format. The matrix printing function is pretty configurable, so we should probably be able to do it. May [1, 2, ..., 11, 12].
On my phone, I can't tell what's going in here, but I only arranged for complex numbers to align on the
Yup, probably. We could also save space by printing floats with no trailing zeros when unnecessary. |
Yeah, this is pretty ugly. What was the former special case? |
Actually the code for it is still there, show.j:262. It just calls On Thu, Oct 20, 2011 at 12:10 AM, Stefan Karpinski
|
Good enough for now? |
I was going to take another crack at the issues you brought up and then close it. I think I can use the core print_matrix function to print vectors and tuples, etc., the way you prefer to have them printed. |
Printing complex numbers like tuples is confusing, and it also doesn't seem to solve the decimal alignment problem. |
Yeah, I was just testing it out. The a + bim format was just too verbose. I have a tentative plan for the alignment issue, it's just hard to do. Will get to it at some point. |
One thing we could consider is having a complex constructor like |
I think thats too terse and takes away names users want. -viral On 08-Dec-2011, at 7:46 AM, JeffBezansonreply@reply.github.com wrote:
|
Yeah, I don't like either of
acceptable even if
is no good, or are they both confusing? I could also see the array version being with just commas even:
It's very compact and the fact that the array type is always printed above makes it a lot less confusing. |
We could also use something like
That's pretty compact, and the fact that the |
I find I like this much better without the parens, since it looks less like a tuple and takes less space too. Technically it still is tuple syntax, but it's not how tuples are printed so it's OK. But I fear we will just have to bite the bullet on this and use |
The |
Another small point: for arrays of arrays we might want to print the elements using |
Also, we might want |
Note that when the float point numbers have exponents the alignment becomes wired julia> rand(Complex128, 5)
5-element Array{Complex{Float64},1}:
0.414626+0.949547im
0.448123+0.498958im
0.0685263+0.76635im
0.131332+0.835893im
0.135788+0.445638im With some exponentials, the alignment is with the last sign (including the signs of the exponent, which should be ignored for the alignment) julia> rand(Complex128, 5).^20
5-element Array{Complex{Float64},1}:
-2.63525-3.56815im
1.22574e-5-1.22442e-5im
1.79063e-7-1.96895e-6im
0.00109833-0.00711597im
-0.00427-0.019812im |
That is really confusing indeed. |
diff --git a/base/show.jl b/base/show.jl
index 5cfa8ac..35252ad 100644
--- a/base/show.jl
+++ b/base/show.jl
@@ -1343,7 +1343,7 @@ function alignment(io::IO, x::Real)
end
"`alignment(1 + 10im)` yields (3,5) for `1 +` and `_10im` (plus sign on left, space on right)"
function alignment(io::IO, x::Complex)
- m = match(r"^(.*[\+\-])(.*)$", sprint(0, show, x, env=io))
+ m = match(r"^(.*[^e][\+\-])(.*)$", sprint(0, show, x, env=io))
m === nothing ? (length(sprint(0, show, x, env=io)), 0) :
(length(m.captures[1]), length(m.captures[2]))
end might work. Will try locally. |
What is left for this issue to be closed? Use spaces between real and imaginary for Complex numbers even in |
Big numbers are still very ugly, try |
I would say that the main reason is because the I choose 80 to fit with the previous example. At the end, I think it is a quite consistent behavior. Should we also crop the strings that span into more than a single line? I don't think so. |
fix not printing already existing packages
I will just go ahead and close this one. The last two reported problems are solved on master ( |
* Add exercise: roman-numerals * Fix config.json add missing comma * Fix roman-numerals example * Fix roman-numerals test sets as suggested * Fix roman-numerals test set cycle as suggested * Remove hint from roman-numerals example * Fix roman-numerals test set Added more samples, added negative number test
Seems this never came to fruition and this is now closed. The printing as of today is pretty good using |
Stdlib: ArgTools URL: https://github.com/JuliaIO/ArgTools.jl.git Stdlib branch: master Julia branch: master Old commit: 08b11b2 New commit: 4eccde4 Julia version: 1.11.0-DEV ArgTools version: 1.1.1 (Does not match) Bump invoked by: @DilumAluthge Powered by: [BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl) Diff: JuliaIO/ArgTools.jl@08b11b2...4eccde4 ``` $ git log --oneline 08b11b2..4eccde4 4eccde4 build(deps): bump actions/checkout from 2 to 3 (#30) 6a4049d build(deps): bump codecov/codecov-action from 1 to 3 (#32) f94a0d3 build(deps): bump actions/cache from 1 to 3 (#31) cb66300 enable dependabot for GitHub actions (#29) ``` Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
…1047) Stdlib: NetworkOptions URL: https://github.com/JuliaLang/NetworkOptions.jl.git Stdlib branch: master Julia branch: master Old commit: f7bbeb6 New commit: 976e51a Julia version: 1.11.0-DEV NetworkOptions version: 1.2.0 (Does not match) Bump invoked by: @DilumAluthge Powered by: [BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl) Diff: JuliaLang/NetworkOptions.jl@f7bbeb6...976e51a ``` $ git log --oneline f7bbeb6..976e51a 976e51a Use human-readable title in the docs (#30) 895aee9 Update ssh-rsa key for github.com (#29) db83efd fix an issue found by JET (#28) ``` Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
* Fix deprecations * WIP: Upgrade to 0.7 * tests pass * tweaks * tweaks * using Some
…d56027 (#54056) Stdlib: ArgTools URL: https://github.com/JuliaIO/ArgTools.jl.git Stdlib branch: release-1.10 Julia branch: backports-release-1.10 Old commit: 08b11b2 New commit: 5d56027 Julia version: 1.10.2 ArgTools version: 1.1.2(Does not match) Bump invoked by: @IanButterworth Powered by: [BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl) Diff: JuliaIO/ArgTools.jl@08b11b2...5d56027 ``` $ git log --oneline 08b11b2..5d56027 5d56027 build(deps): bump julia-actions/setup-julia from 1 to 2 (#38) b6189c7 build(deps): bump codecov/codecov-action from 3 to 4 (#37) 997089b fix tests for TEMP_CLEANUP, which might be a Lockable (#35) 4a5f003 build(deps): bump actions/cache from 3 to 4 (#36) 84ba9e8 Hardcode doc edit backlink (#34) 9238839 build(deps): bump actions/checkout from 3 to 4 (#33) 4eccde4 build(deps): bump actions/checkout from 2 to 3 (#30) 6a4049d build(deps): bump codecov/codecov-action from 1 to 3 (#32) f94a0d3 build(deps): bump actions/cache from 1 to 3 (#31) cb66300 enable dependabot for GitHub actions (#29) ``` Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
Our array printing is currently quite embarrassing. It needs to be fixed.
The text was updated successfully, but these errors were encountered: