查找数组之间双射的算法

发布于 2024-11-13 08:14:29 字数 154 浏览 6 评论 0原文

我有两个数组,例如 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 技术交流群。

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

发布评论

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

评论(3

尬尬 2024-11-20 08:14:29

我认为这个问题没有通用的解决方案。您可以尝试 FindSequenceFunction,但它并不总能找到解决方案。对于当前的情况,您需要更长的列表:

In[250]:= FindSequenceFunction[Transpose[{{1, 2, 3}, {2, 4, 8}}], n]

Out[250]= FindSequenceFunction[{{1, 2}, {2, 4}, {3, 8}}, n]

但是

In[251]:= FindSequenceFunction[Transpose[{{1, 2, 3, 4}, {2, 4, 8, 16}}], n]

Out[251]= 2^n

如果您对双射有一些猜测,您也可以使用 FindFit:

In[252]:= FindFit[Transpose[{{1, 2, 3}, {2, 4, 8}}], p*q^x, {p, q}, x]

Out[252]= {p -> 1., q -> 2.}

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:

In[250]:= FindSequenceFunction[Transpose[{{1, 2, 3}, {2, 4, 8}}], n]

Out[250]= FindSequenceFunction[{{1, 2}, {2, 4}, {3, 8}}, n]

but

In[251]:= FindSequenceFunction[Transpose[{{1, 2, 3, 4}, {2, 4, 8, 16}}], n]

Out[251]= 2^n

You can also play with FindFit, if you have some guesses about the bijection:

In[252]:= FindFit[Transpose[{{1, 2, 3}, {2, 4, 8}}], p*q^x, {p, q}, x]

Out[252]= {p -> 1., q -> 2.}
不离久伴 2024-11-20 08:14:29

正如其他人所说,这个问题的定义不明确。

给出相同结果的其他可能的函数有(可能还有无数其他函数): (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。

我发现这些解决方案使用:

n = 5; (* try various other values *)
A = {1, 2, 3} ; B = {2, 4, 8}
eqs = Table[
  Sum[a[i] x[[1]]^i, {i, n}] == x[[2]], {x, {A, B}\[Transpose]}]
sol = Solve[eqs, Table[a[i], {i, n}], Reals]
Sum[a[i] x^i, {i, n}] /. sol

有时并非所有 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:

n = 5; (* try various other values *)
A = {1, 2, 3} ; B = {2, 4, 8}
eqs = Table[
  Sum[a[i] x[[1]]^i, {i, n}] == x[[2]], {x, {A, B}\[Transpose]}]
sol = Solve[eqs, Table[a[i], {i, n}], Reals]
Sum[a[i] x^i, {i, n}] /. sol

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]

逆夏时光 2024-11-20 08:14:29

既然您标记了 Mathematica,我将使用 Mathematica 函数作为参考。

如果您对使用平滑函数任意拟合数据感兴趣,则可以使用插值。例如

a = {1, 2, 3}; b = {2, 4, 8};
f = Interpolation[Transpose[{a, b}]];

(* Graph the interpolation function *)
Show[Plot[f[x], {x, 1, 3}], Graphics[Point /@ Transpose[{a, b}]], 
   PlotRange -> {{0, 4}, {0, 9}}, Frame -> Automatic, Axes -> None]

Plot of f

插值使用分段多项式。如果您碰巧了解或愿意学习一些数值方法(尤其是 B 样条曲线),您可以使用您最喜欢的编程语言执行相同的操作。

相反,如果您对数据有所了解,例如数据的形式为 cd^x,那么您可以进行最小化来查找未知数(在本例中为 c 和 d)。如果您的数据实际上是从 cd^x 形式生成的,那么拟合将是公平的,否则误差在最小二乘意义上被最小化。因此,对于您的数据:

FindFit[Transpose[{a, b}], c d^x, {c, d}, {x}]

报告:

{c -> 1., d -> 2.}

表明您的函数是 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.

a = {1, 2, 3}; b = {2, 4, 8};
f = Interpolation[Transpose[{a, b}]];

(* Graph the interpolation function *)
Show[Plot[f[x], {x, 1, 3}], Graphics[Point /@ Transpose[{a, b}]], 
   PlotRange -> {{0, 4}, {0, 9}}, Frame -> Automatic, Axes -> None]

Plot of f

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:

FindFit[Transpose[{a, b}], c d^x, {c, d}, {x}]

reports:

{c -> 1., d -> 2.}

Indicating that your function is 2^x, just as you knew all along.

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