曼德勃罗集数组编程
Array-programming the Mandelbrot set

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

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

## Hacker News 上关于 Uiua 和数组编程的讨论 最近 Hacker News 上进行了一场关于使用数组编程语言 Uiua (uiua.org) 实现曼德勃罗集的新颖实现的讨论。发帖者强调 Uiua 是一种独特的工具,结合了面向数组的编程和基于栈的范式。 评论者普遍认为 Uiua “非常酷”,赞扬其数据驱动的方法以及即使对于不熟悉数组编程的人来说,也能实现优雅解决方案的潜力。 几位用户指出,需要转变思维去“用数组思考”,这对于更广泛的编程技能是有益的,尤其是在图形和着色器编程方面。 讨论还涉及了高度简洁的数组代码固有的可读性挑战,将其与正则表达式进行了比较。 虽然有些人认为在数组语言中进行大型项目是不切实际的,但也有人分享了使用大型 K 代码库的经验,虽然最初很简洁,但会随着时间的推移加入注释和描述性命名以提高可维护性。 最终,共识是,虽然功能强大,但数组语言可能更适合原型设计和专注的算法任务,而不是大规模的协作项目。
相关文章

原文
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