曼德勃罗集数组编程
Array Programming the Mandelbrot Set

原始链接: https://jcmorrow.com/mandelbrot/

作者重温了一段用数组语言J编写的曼德勃罗集程序,发现时隔一段时间后已难以阅读。与其重新学习J,他们决定将其翻译成Uiua,另一种他们最近更熟悉(但也有点生疏)的数组语言。 Uiua的基于栈的特性显著改变了程序结构,与J的从右到左求值方式不同。作者欣赏Uiua在栈操作方面更清晰的函数签名。Uiua的一个突出特点是它可以自动从程序输出生成GIF,增强了语言的即时性。 作者强调了数组语言的核心优势——快速探索复杂的变换——以及Uiua如何通过自动可视化来提升这一优势,简化了通常繁琐的输出渲染过程,使体验感觉非常现代化。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 用数组编程曼德勃罗集 (jcmorrow.com) 6点 由 jcmorrow 1小时前 | 隐藏 | 过去 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文
home

Years ago, when I was learning the J programming language I wrote the mandelbrot set:

load 'viewmat'
 
mandelbrot =. ([ + ^&2)
mandel_iter =: (|)@:(mandelbrot/\.)@:([ # ])
range =. (i.@:(1&+)@:(*&2) - ])
grid =. ({. (,"0)/ {.)@:(range "0)@:(2&#)
 
pixels =. (2&>)@:(25&mandel_iter)@:(-&0.5)@:j./@:(0.01&*) L:0 <"1 grid 100
 
viewmat > +/ L:0 pixels

As is often the case with array programming, I revisited this last week only to realize I couldn’t read it. I considered revisiting it to brush up on J, but in more recent times I’ve been doing my array programming in Uiua. While I’m also rusty at Uiua these days, I thought that a translation would be a more interesting project than just remembering how J works. After brushing up on Uiua’s documentation (and rediscovering that LLM’s are useless with Uiua because it’s such a strange language and so far out-of-distribution) I did finally manage a translation:

Square     ← × ⟜∘
Mandelbrot ← + Square
R          ← 1000
 
× ÷ R 2.5 -×0.75 R [⍥(⇡ R) R]
ℂ ⍉ × ÷ R 2.5 -×0.5 R [⍥(⇌ ⇡ R) R]
 
<2 ⌵ \Mandelbrot ↯ 25

The fact that Uiua is not only an array language, but a stack-based array language really changes how some of the program reads. J also evaluates right-to-left, but it has much less native support for doing things like manually coordinating the stack. Plus, while dyadic verbs in J are kind of neat, I much prefer the signatures of Uiua, which look much like signatures in other stack-based languages (i.e. will this function remove things from the stack, or push things to the stack, or leave the stack the same depth as when it was called?).

There is one other thing to note about Uiua’s implementation: when executed in an interactive environment Uiua automatically turns that data into a GIF.

The mandelbrot set being iterated into view one frame at a time.

This is just such a cool feature. The advantage of array languages in my experience has always been their immediacy. When a complex transformation is only a few keystrokes away you can cover a lot of intellectual distance very quickly. Uiua takes that to a new extreme by also figuring out the best way to display your output to you automatically. This is such a basic idea in some ways, but to me, as someone who spends a few minutes every time I want to start a new visual arts project remembering how to just render some pixels to the screen, it feels like living in the future.

home
联系我们 contact @ memedata.com