查找数组之间双射的算法
我有两个数组,例如 A={1, 2, 3}
和 B={2, 4, 8}
(数组项计数和数量可能会有所不同)。如何找到数组之间的双射。
在这种情况下,它将是 f:A->B; f(x)=2^(x)
I have two arrays, say A={1, 2, 3}
and B={2, 4, 8}
(array item count and numbers may vary). How do I find a bijection between the arrays.
In this case, it would be f:A->B; f(x)=2^(x)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这个问题没有通用的解决方案。您可以尝试 FindSequenceFunction,但它并不总能找到解决方案。对于当前的情况,您需要更长的列表:
但是
如果您对双射有一些猜测,您也可以使用 FindFit:
I don't think this problem has a general solution. You may try FindSequenceFunction, but it will not always find the solution. For the case at hand, you'd need a bit longer lists:
but
You can also play with
FindFit
, if you have some guesses about the bijection:正如其他人所说,这个问题的定义不明确。
给出相同结果的其他可能的函数有(可能还有无数其他函数): (8 x)/3 - x^2 + x^3/3, x + (37 x^2)/18 - (4 x^3) /3 + (5 x^4)/18 和 (259 x^3)/54 - (31 x^4)/9 + (35 x^5)/54。
我发现这些解决方案使用:
有时并非所有 a[i] 都完全确定,您可能会想出自己的值。
[提示:在 Mathematica 中最好不要使用以大写字母开头的变量,以免与保留字发生冲突]
As others have remarked, this problem is ill-defined.
Other possible functions that give the same results are (among probably infinite others): (8 x)/3 - x^2 + x^3/3, x + (37 x^2)/18 - (4 x^3)/3 + (5 x^4)/18, and (259 x^3)/54 - (31 x^4)/9 + (35 x^5)/54.
I found these solutions using:
Sometimes not all of the a[i]'s are fully determined and you may come up with values of your own.
[tip: better not use variables starting with a capital letter in Mathematica so as not to get into conflict with reserved words]
既然您标记了 Mathematica,我将使用 Mathematica 函数作为参考。
如果您对使用平滑函数任意拟合数据感兴趣,则可以使用插值。例如
插值使用分段多项式。如果您碰巧了解或愿意学习一些数值方法(尤其是 B 样条曲线),您可以使用您最喜欢的编程语言执行相同的操作。
相反,如果您对数据有所了解,例如数据的形式为 cd^x,那么您可以进行最小化来查找未知数(在本例中为 c 和 d)。如果您的数据实际上是从 cd^x 形式生成的,那么拟合将是公平的,否则误差在最小二乘意义上被最小化。因此,对于您的数据:
报告:
表明您的函数是 2^x,正如您一直知道的那样。
Since you tag Mathematica, I'll use Mathematica functions as a reference.
If you are interested in an arbitrary fit of your data with a smooth function, you can use Interpolation. E.g.
Interpolation uses piecewise polynomials. You can do the same in your favorite programming language if you happen know or are willing to learn a bit about numerical methods, especially B-Splines.
If instead you know something about your data, e.g. that it is of the form c d^x, then you can do a minimization to find the unknowns (c and d in this case). If your data is in fact generated from the form c d^x, then the fit will be fairly, otherwise it's the error is minimized in the least-squares sense. So for your data:
reports:
Indicating that your function is 2^x, just as you knew all along.