在球拍上取一个函数的倒数。

时间:2022-06-30 20:57:21

I am trying to write a higher-order Racket function that takes a first-order function of one variable and returns its inverse. I know that it has to start off something like this:

我试着写一个高阶的球拍函数,它接受一个变量的一阶函数,然后返回它的逆。我知道它必须这样开始:

(let [(inverse (lambda (f)
                 (lambda (y)
                   ... )))])

I figured this because inverse must take a function which returns a function which takes a y and returns x such that (= (f x) y). In other words, the contract for inverse is something like:

我想这是因为逆必须取一个函数,它返回一个函数,这个函数取一个y,然后返回x (= (f x) y),换句话说,逆的契约是这样的:

; inverse : (number? -> number?) -> (number? -> number?)

I'm just stumped trying to figure out what goes where the elipses are?

我只是想弄明白什么是省略号?

EDIT: In response to people saying this is impossible, I am willing to accept an inverse function that when given y returns a possible x. In response to comments about the function not having an inverse, please note the contract that I have for f. It is a (number? -> number?) mapping, and therefore has an inverse.

编辑:为了回应人们说这是不可能的,我愿意接受一个逆函数,当给定y返回一个可能的x时,在回应关于没有逆的函数的评论时,请注意我对f的契约,它是一个(数字?)->数字?)映射,因此有一个逆。

3 个解决方案

#1


6  

For the general case, given an arbitrary function f you can't tell what's its inverse function. Even worse, a given function might not have an inverse at all - for example: the input function could perform an MD5 hash, which has no inverse. Sorry, your question has no answer.

对于一般情况,给定一个任意函数f你不知道它的逆函数是什么。更糟糕的是,一个给定的函数可能根本没有逆函数——例如:输入函数可以执行MD5散列,它没有逆。对不起,你的问题没有答案。

#2


3  

Consider f(x)=x^2. This is a very simple function without an inverse. (because f(1)=f(-1) there are no unique inverse to y=1).

考虑f(x)= x ^ 2。这是一个非常简单的函数,没有逆矩阵。(因为f(1)=f(-1) y=1没有唯一的逆。

Since a very simple function might have no inverse, you cant't expect a general Scheme function to have an inverse.

由于一个非常简单的函数可能没有逆函数,所以你不能期望一般的Scheme函数有一个逆。

#3


2  

I knew that I had seen this before, but I couldn't remember how it worked. Now I remember, but I realized that I had been misleading in my question because the version I had seen assumed that we already had a function called root which would return one of the zeros of a provided function. Given that function, it is pretty easy:

我知道我以前见过这个,但我不记得它是怎么工作的。现在我想起来了,但是我意识到我在我的问题上误导了,因为我看到的版本假设我们已经有了一个叫做root的函数,它会返回一个提供函数的一个0。考虑到这个函数,它很简单:

(define (inverse f)
  (lambda (y)
    (root (lambda (x) (- (f x) y)))))

It's pretty easy to see how this works. The inverse of a function is the x such that f(x) = y. Obviously, the root of the function f(x) - y = 0 is that x.

很容易看出它是如何工作的。函数的逆是x, f(x) = y,显然,函数f(x) - y = 0的根是x。

The place where I had gone wrong is that the best we can do for root is Newton's method or some other approximation.

我做错的地方是我们能做的最好的根是牛顿法或者其他近似。

#1


6  

For the general case, given an arbitrary function f you can't tell what's its inverse function. Even worse, a given function might not have an inverse at all - for example: the input function could perform an MD5 hash, which has no inverse. Sorry, your question has no answer.

对于一般情况,给定一个任意函数f你不知道它的逆函数是什么。更糟糕的是,一个给定的函数可能根本没有逆函数——例如:输入函数可以执行MD5散列,它没有逆。对不起,你的问题没有答案。

#2


3  

Consider f(x)=x^2. This is a very simple function without an inverse. (because f(1)=f(-1) there are no unique inverse to y=1).

考虑f(x)= x ^ 2。这是一个非常简单的函数,没有逆矩阵。(因为f(1)=f(-1) y=1没有唯一的逆。

Since a very simple function might have no inverse, you cant't expect a general Scheme function to have an inverse.

由于一个非常简单的函数可能没有逆函数,所以你不能期望一般的Scheme函数有一个逆。

#3


2  

I knew that I had seen this before, but I couldn't remember how it worked. Now I remember, but I realized that I had been misleading in my question because the version I had seen assumed that we already had a function called root which would return one of the zeros of a provided function. Given that function, it is pretty easy:

我知道我以前见过这个,但我不记得它是怎么工作的。现在我想起来了,但是我意识到我在我的问题上误导了,因为我看到的版本假设我们已经有了一个叫做root的函数,它会返回一个提供函数的一个0。考虑到这个函数,它很简单:

(define (inverse f)
  (lambda (y)
    (root (lambda (x) (- (f x) y)))))

It's pretty easy to see how this works. The inverse of a function is the x such that f(x) = y. Obviously, the root of the function f(x) - y = 0 is that x.

很容易看出它是如何工作的。函数的逆是x, f(x) = y,显然,函数f(x) - y = 0的根是x。

The place where I had gone wrong is that the best we can do for root is Newton's method or some other approximation.

我做错的地方是我们能做的最好的根是牛顿法或者其他近似。