使用 GIMP 方案编写矩阵转换

发布于 2024-11-03 17:03:26 字数 261 浏览 3 评论 0原文

我想用 DrRacket(R5RS 但不仅如此)写一些漂亮的数学(Racket 标签有点空)。

我真的很想编写一些矩阵的东西,例如:

(3 3 3) (5 3 4) (4 4 4) -> (5 3 4) (5 5 5) (5 3 4)

以及其他类似的东西来设置一些漂亮的 gimp 过滤器...

有些人指出,这可以通过列表内的列表来完成,但我在这里想不出一个实际的例子...

我期待您的回复。 此致,Andreas_P

I want to write some nice Math with DrRacket(R5RS but not only) (Racket Tag is a bit empty).

I would really like to code some matrix stuff like:

(3 3 3) (5 3 4)
(4 4 4) -> (5 3 4)
(5 5 5) (5 3 4)

And other stuff like this to set up some nice gimp filters...

Some folks pointed out, this could be done via lists inside lists, but I can't think of a practical example here...

I am looking forward to your reply.
Yours sincerely, Andreas_P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走野 2024-11-10 17:03:26

一些注意事项:

1)Scheme“内置于核心”是什么意思? GIMP 现在拥有原生 Python-fu 和 Script-fu

2) 赢得 Google AI Challenge 的 Lisp 是 Common Lisp,而不是 Scheme

3) 我不确定,但 Script-fu 控制台声明 TinyScheme,因此我希望它能够就更完整的方案实现而言,在几乎没有库支持的情况下,这确实是必不可少的

无论如何,我尝试了一些关于矩阵“方案方式”的示例。为了简单起见,它们对输入数据缺乏任何控制,但对于简单的示例,它们在 DrRacket 上运行良好。

(define (vect-sum x y)
  (cond 
    ((empty? x) empty)
    (else 
     (cons (+ (first x) (first y)) (vect-sum (rest x) (rest y))))))

(define (scalar-prod a v)
  (cond
    ((empty? v) empty)
    (else
     (cons (* a (first v)) (scalar-prod a (rest v))))))

(define (matr-sum x y)
  (cond
    ((empty? x) empty)
    (else
     (cons (vect-sum (first x) (first y)) 
           (matr-sum (rest x) (rest y))))))

(define (matr-scalar-prod a m)
  (cond
    ((empty? m) empty)
    (else
     (cons (scalar-prod a (first m)) (matr-scalar-prod a (rest m))))))

现在对数据进行简单测试,如其他答案所示:

> (define m '((3 3 3)(5 3 4)(4 4 4)))
> m
'((3 3 3) (5 3 4) (4 4 4))
> (matr-scalar-prod 3 m)
'((9 9 9) (15 9 12) (12 12 12))

A few notes:

1) what do you mean with Scheme "is built in within the core"? GIMP now has a native Python-fu along with Script-fu

2) the Lisp that won Google AI Challenge is Common Lisp, not Scheme

3) I am not sure, but the Script-fu console states TinyScheme, thus I would expect it to be really essential, with little library support, with respect to more complete Scheme implementations

Anyway, I tried a few example on matrices "the Scheme way". For the sake of simplicity, they lack any control on the input data, but for simple examples they work fine on DrRacket.

(define (vect-sum x y)
  (cond 
    ((empty? x) empty)
    (else 
     (cons (+ (first x) (first y)) (vect-sum (rest x) (rest y))))))

(define (scalar-prod a v)
  (cond
    ((empty? v) empty)
    (else
     (cons (* a (first v)) (scalar-prod a (rest v))))))

(define (matr-sum x y)
  (cond
    ((empty? x) empty)
    (else
     (cons (vect-sum (first x) (first y)) 
           (matr-sum (rest x) (rest y))))))

(define (matr-scalar-prod a m)
  (cond
    ((empty? m) empty)
    (else
     (cons (scalar-prod a (first m)) (matr-scalar-prod a (rest m))))))

And now a simple test on the data as in the other answer:

> (define m '((3 3 3)(5 3 4)(4 4 4)))
> m
'((3 3 3) (5 3 4) (4 4 4))
> (matr-scalar-prod 3 m)
'((9 9 9) (15 9 12) (12 12 12))
我不在是我 2024-11-10 17:03:26

HHi,我强烈建议您使用Python 而不是Scheme 来编写GIMP 脚本,除非您想为了娱乐目的而学习Scheme。

Python 的原则之一是不要让语言成为你和问题之间的障碍,编写你自己的矩阵操作代码是微不足道的。如果您想要高性能操作,您可以使用第三方库,例如 NumPy(甚至来自 GIMP 环境内部)来获取它。

因此,对于允许标量乘法和加一的 Matrix 类,可以简单地编写:

class Matrix(object):
    def __init__(self, rows, cols, *args):
        self.rows = rows
        self.cols = cols
        self.values = args

    def __getitem__(self, (i,j)):
        return self.values[i * self.cols + j]

    def __setitem__(self,(i,j), value):
        self.values[i * self.cols + j] = value

    def __add__(self, other):
        values = []
        for i in range(self.rows):
            for j in range(self.cols):
                values.append(self[i,j] + other[i,j])
        return Matrix(self.rows, self.cols, *values)

    def __mul__(self, N):
        return Matrix(self.rows, self.cols,*(N * v for v in self.values))

    def __repr__(self):
        return "\n".join (" ".join(str(self[i,j]) for j in range(self.cols))   for i in range(self.rows) ) 

Python 交互式控制台上的示例:

>>> m = Matrix(3,3,
... 3,3,3,
... 5,3,4,        
... 4,4,4)    
>>> m * 3
9 9 9
15 9 12
12 12 12

实现更多操作同样简单,并且,为了调用 GIMP 的 API 函数,在这个示例类中,您可以只使用 m。值,这只是一个按顺序包含所有矩阵值的列表 - 这是 GIMP PDB 函数使用它们的方式。 (例如pdb.gimp_drawable_transform_matrix或pdb.plug_in_convmatrix。(我想你已经在帮助菜单下找到了GIMP的API浏览器 - 在Scheme和Python中也有相同的功能,只需将名称中的“-”替换为“_” )

HHi, I'd highly recommend you to use Python instead of Scheme for writing GIMP scripts, unless you want to learn Scheme for recreational purposes.

One of the tenets in Python is to not allow the language to stay between you and your problem, and writing your own Matrix manipulation code is trivial. If you want high performance ops, you can use a third party library such as NumPy (even from inside GIMP environment) to get it.

So, for a Matrix class that would allow scallar multiplication and adding one could simply write:

class Matrix(object):
    def __init__(self, rows, cols, *args):
        self.rows = rows
        self.cols = cols
        self.values = args

    def __getitem__(self, (i,j)):
        return self.values[i * self.cols + j]

    def __setitem__(self,(i,j), value):
        self.values[i * self.cols + j] = value

    def __add__(self, other):
        values = []
        for i in range(self.rows):
            for j in range(self.cols):
                values.append(self[i,j] + other[i,j])
        return Matrix(self.rows, self.cols, *values)

    def __mul__(self, N):
        return Matrix(self.rows, self.cols,*(N * v for v in self.values))

    def __repr__(self):
        return "\n".join (" ".join(str(self[i,j]) for j in range(self.cols))   for i in range(self.rows) ) 

Example on Python's interactive console:

>>> m = Matrix(3,3,
... 3,3,3,
... 5,3,4,        
... 4,4,4)    
>>> m * 3
9 9 9
15 9 12
12 12 12

Implementing more operations is equally simple, and, for calling GIMP's API functions, with this example class, you can just use m.values, that is simply a list with all the matrix values in sequence - which is the way GIMP PDB's functions use them. (Such as pdb.gimp_drawable_transform_matrix or pdb.plug_in_convmatrix. (I suppose you've found GIMP's API browser under the help menu - the same functions are available in Scheme and in Python, just replace the "-" in the names for "_")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文