问题描述
在附言中, roll
运算符非常笼统,难以可视化。您如何确定自己朝着正确的方向滚动?
In postscript, the roll
operator is very general and difficult to visualize. How do you make sure you're rolling in the right direction?
我想在 roll
因为我希望能够使用变量来转换函数
I want to get a solid handle on roll
because I want to be able to transform functions using variables
/f { % x y z
/z exch def
/y exch def
/x exch def
x dup mul
y dup mul
z dup mul add add % x^2+y^2+z^2
} def
使用堆栈操作进入函数,更像是
into functions using stack manipulations, more like
/f { % x y z
3 1 roll dup mul % y z x^2
3 1 roll dup mul % z x^2 y^2
3 1 roll dup mul % x^2 y^2 z^2
add add % x^2+y^2+z^2
} def
或
/f { % x y z
3 { 3 1 roll dup mul } repeat
2 { add } repeat % x^2+y^2+z^2
} bind def
这些都应通过减少名称查找(散列表搜索)来更快地执行。
These should both execute faster by having fewer name-lookups (hashtable search).
使用卷
我总是必须进行测试;而且我通常
第一次尝试会弄错!我对exch没问题,不过
With roll
I always have to test it out; and I usuallyget it wrong on the first try! I'm okay with exch, though
推荐答案
我很长一段时间都无法滚动。我记得现在使用这些方法,它们都是等效的:
I had difficulty with roll for a very long time. I remember it now using these ways, which are all equivalent:
-
正j,滚开
positive j, to roll away
7 8 9 3 1 roll % 9 7 8
负值,取回(或取反,然后取回)
negative, to get it back (or "negateeve, to then retrieve")
% 9 7 8 3 -1 roll % 7 8 9
也许是一个更好的方式来思考它是物理堆栈
(例如书籍),所以堆栈的顶部实际上是在顶部。Perhaps a better way to think of it is a physical stack(of books, say) so the top of stack is literally "on top".
然后积极的卷动上升:
for j number of times pick up n books put the top one on the bottom (shifting the substack "up") put them back down
然后负向下降:
for j number of times pick up n books put the bottom one on top (shifting the substack "down") put them back down
sideways
但是我通常将堆放在侧面,将对象
在文件中看起来像一个序列文字。因此,我认为
的正面滚动是将第j个
事物后面的前j个事物隐藏起来;负数表示从第n个事物开始抓取以
开头的j个事物。给予和接受。sideways
But I usually picture the stack sideways, the way the objectswould look in a file as a sequence of literals. So I think ofthe positive roll as stashing the top j things behind the nththing; and the negative roll as snagging j things startingwith the nth thing. Give and Take.
离开。
n j roll __ j > 0 __ move top j elements to the bottom of n n TOS -------------| | j | | -----| | | | V V | a b c d e f g h ^ | | | |-------| ^ | -<-<-<-<-<- move
然后返回。
__ j < 0 __ move j elements from the bottom of n to the top n TOS -------------| | j | |----- | | | | V V | a b c d e f g h | | ^ |-------| | | ^ ->->->->->- move
棉绒滚子
还有另一种方法是将其横向放置,并在顶部放置一个粘性轮(也许是短绒辊)
lint-roller
Still another way is to picture it sideways, and laying a sticky wheel on top (a lint-roller, maybe)
(a) (b) (c) (d) (e) 5 3 roll _______ / \ | 3 | | / | \ | \_______/ (a) (b) (c) (d) (e)
然后积极滚动
_______ (e) / / \ | 3 --| (d) | \ | \_______/ (c) (a) (b) (e)__(d)__(c) /\ | /\ | 3 | | | \_______/ (a) (b) (c)_______ /\ \ (d) |-- 3 | |/ | \_______/ (e) (a) (b) _______ / \ | 3 | | / | \ | \_______/ (c) (d) (e) (a) (b)
_______ / \ | 3 | | / | \ | \_______/ (a) (b) (c) (d) (e) (a)_______ /\ \ (b) |-- 3 | |/ | \_______/ (c) (d) (e) (c)__(b)__(a) /\ | /\ | 3 | | | \_______/ (d) (e) _______ (c) / / \ | 3 --| (b) | \ | \_______/ (a) (d) (e) _______ / \ | 3 | | / | \ | \_______/ (d) (e) (a) (b) (c)
消除负值
不难发现负向滚动完全没有必要,因为如果j
eliminate the negative
It shouldn't be difficult to see that negative rolls are entirely unnecessary because if j<0, it can be replaced by n-j. eg.
3 -1 roll % roll bottom 1 element from 3 to the top 3 2 roll % roll top 2 elements behind the 3rd
后面的两个元素是相同的。
are the same.
16 -4 roll % roll bottom 4 elements from 16 to the top 16 12 roll % roll top 12 elements behind the 16th
相同。
这将导致最终的,最终的简化视图(尽管上述每个方法也可以使用)。
This leads to the final, ultimate simplified view (though each of the above will work, too).
您实际上只是与下面的 nj 元素交换 顶部的 j 元素
You're really just exchanging the top j elements with the n-j elements below that.
说您在堆栈上有这个烂摊子(其中$ TOS $标记了堆栈的顶部),并想正确地订购它:
Say you have this mess on the stack (where $TOS$ marks the top of the stack), and want to order it properly:
g h i j k l m n o p q r s t u v w x y z a b c d e f $TOS$
向上(向下)计数 n 和 j 。
g h i j k l m n o p q r s t u v w x y z a b c d e f 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 | | j = 6 . . . . | n = 26 . . . . . . . . . . . . . . . . . . . . . . . > 26 6 roll pstack a b c d e f g h i j k l m n o p q r s t u v w x y z
j的负值只需将分隔线相对于 n 元素中最深元素的位置(从下面开始计数)即可。
A negative value for j simply positions that dividing line relative to the deepest element from among the n elements (it counts from below).
t u v w x y z a b c d e f g h i j k l m n o p q r s 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 . . . . j = -7 | | . . . . . . . . . . . . . . . . . . . . . . . n = 26 | > 26 -7 roll pstack a b c d e f g h i j k l m n o p q r s t u v w x y z
这是一个便利功能,它提供了一个类似于大交换视图的滚动界面。
% r0..rN s0..sM N M swap s0..sM r0..rN % a gentler interface to the power of roll /swap { exch 1 index add exch roll } def 0 1 2 3 /a /b /c 4 3 swap pstack
输出:
GPL Ghostscript 8.62 (2008-02-29) Copyright (C) 2008 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. 3 2 1 0 /c /b /a GS<7>GS<7>
这篇关于您如何保持“ roll”运算符笔直?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!