Mathematica 地图问题

发布于 2024-10-01 08:57:30 字数 316 浏览 2 评论 0原文

原始问题:

我知道 Mathematica 有一个内置的 map(f, x),但是这个函数是什么样的?我知道您需要查看列表中的每个元素。

有什么帮助或建议吗?

编辑(由 Jefromi,根据 Mike 的评论拼凑而成):

我正在开发一个需要在地图等列表中移动的程序,但我不允许使用它。我也不被允许使用 Table;我需要在没有其他功能帮助的情况下浏览列表。我正在开发一个递归版本,我有一个空列表,但是在其中包含项目的列表中移动是行不通的。这是我的第一个案例: newMap[#, {}] = {} (空列表的地图只是一个空列表)

Original question:

I know Mathematica has a built in map(f, x), but what does this function look like? I know you need to look at every element in the list.

Any help or suggestions?

Edit (by Jefromi, pieced together from Mike's comments):

I am working on a program what needs to move through a list like the Map, but I am not allowed to use it. I'm not allowed to use Table either; I need to move through the list without help of another function. I'm working on a recursive version, I have an empty list one down, but moving through a list with items in it is not working out. Here is my first case: newMap[#, {}] = {} (the map of an empty list is just an empty list)

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

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

发布评论

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

评论(2

深爱成瘾 2024-10-08 08:57:30

我发布了一个递归解决方案,但后来决定将其删除,因为从评论来看,这听起来像是一个家庭作业问题,而我通常是一个授之以鱼的人。

您将使用定义 newMap[f_, {}] := {} 获得递归解决方案。

Mathematica 的模式匹配是您的朋友。考虑如何实现 newMap[f_, {e_}] 的定义,并从那里实现 newMap[f_, {e_, rest___}]

最后一个提示:一旦您可以定义最后一个函数,您实际上就不需要 {e_} 的情况了。

更新

根据您的评论,也许这个示例将帮助您了解如何应用任意函数:

func[a_, b_] := a[b]

In[4]:= func[Abs, x]
Out[4]= Abs[x]

解决方案

既然OP抓到了一条鱼,可以这么说,(恭喜! )这里有两个递归解决方案,以满足任何旁观者的好奇心。第一个可能是我认为“惯用”的 Mathematica:

map1[f_, {}] := {}
map1[f_, {e_, rest___}] := {f[e], Sequence@@map1[f,{rest}]}

这是一种不太充分利用模式匹配的方法,这基本上就是 OP 最终得到的结果:

map2[f_, {}] := {}
map2[f_, lis_] :=  {f[First[lis]], Sequence@@map2[f, Rest[lis]]}

{f[e], Sequence@ @map[f,{rest}]}部分可以用多种等价方式表达,例如:

  • Prepend[map[f, {rest}], f[e]]
  • Join[{f[e]}, map[f, {rest}] (@Mike 使用了此方法)
  • Flatten[{{f[e]}, map[f, {休息}]},1]

我将留给读者更多的思考,并思考其中大多数的性能影响=)

最后,为了好玩,这里有一个程序版本,尽管写这让我有点恶心:;-)

map3[f_, lis_] :=
 (* copy lis since it is read-only *)
 Module[{ret = lis, i},
  For[i = 1, i <= Length[lis], i++,
   ret[[i]] = f[lis[[i]]]
   ];
  ret
  ]

I posted a recursive solution but then decided to delete it, since from the comments this sounds like a homework problem, and I'm normally a teach-to-fish person.

You're on the way to a recursive solution with your definition newMap[f_, {}] := {}.

Mathematica's pattern-matching is your friend. Consider how you might implement the definition for newMap[f_, {e_}], and from there, newMap[f_, {e_, rest___}].

One last hint: once you can define that last function, you don't actually need the case for {e_}.

UPDATE:

Based on your comments, maybe this example will help you see how to apply an arbitrary function:

func[a_, b_] := a[b]

In[4]:= func[Abs, x]
Out[4]= Abs[x]

SOLUTION

Since the OP caught a fish, so to speak, (congrats!) here are two recursive solutions, to satisfy the curiosity of any onlookers. This first one is probably what I would consider "idiomatic" Mathematica:

map1[f_, {}] := {}
map1[f_, {e_, rest___}] := {f[e], Sequence@@map1[f,{rest}]}

Here is the approach that does not leverage pattern matching quite as much, which is basically what the OP ended up with:

map2[f_, {}] := {}
map2[f_, lis_] :=  {f[First[lis]], Sequence@@map2[f, Rest[lis]]}

The {f[e], Sequence@@map[f,{rest}]} part can be expressed in a variety of equivalent ways, for example:

  • Prepend[map[f, {rest}], f[e]]
  • Join[{f[e]}, map[f, {rest}] (@Mike used this method)
  • Flatten[{{f[e]}, map[f, {rest}]}, 1]

I'll leave it to the reader to think of any more, and to ponder the performance implications of most of those =)

Finally, for fun, here's a procedural version, even though writing it made me a little nauseous: ;-)

map3[f_, lis_] :=
 (* copy lis since it is read-only *)
 Module[{ret = lis, i},
  For[i = 1, i <= Length[lis], i++,
   ret[[i]] = f[lis[[i]]]
   ];
  ret
  ]
心在旅行 2024-10-08 08:57:30

为了回答您在评论中提出的问题,Map 中的第一个参数是一个接受单个参数的函数。这可以是纯函数,也可以是已经只接受单个参数的函数的名称,例如

In[1]:=f[x_]:= x + 2
       Map[f, {1,2,3}]
Out[1]:={3,4,5}

As to how to replacement Map with a recursive function of your own deving ... 按照 Jefromi 的示例,我我不会透露太多,因为这是家庭作业。但是,您显然需要某种方式对列表的一部分进行操作,同时保持列表的其余部分完整,以便映射函数的递归部分。正如他所说,部分< /a> 是一个很好的起点,但我会查看它引用的一些其他函数,看看它们是否更有用,例如 FirstRest。另外,我还可以看到 Flatten 在哪里有用。最后,您需要一种结束递归的方法,因此学习如何约束模式可能会很有用。顺便说一句,这可以通过一行或两行完成,具体取决于您是否为地图创建第二个定义(更简单的方法)。

提示:现在您已经有了结束条件,您需要回答三个问题:

  1. 如何从列表中提取单个元素,
  2. 如何引用列表的其余元素,以及
  3. 如何把它放回去?

思考流程中的一个步骤以及在该步骤中需要完成什么任务会有所帮助。

To answer the question you posed in the comments, the first argument in Map is a function that accepts a single argument. This can be a pure function, or the name of a function that already only accepts a single argument like

In[1]:=f[x_]:= x + 2
       Map[f, {1,2,3}]
Out[1]:={3,4,5}

As to how to replace Map with a recursive function of your own devising ... Following Jefromi's example, I'm not going to give to much away, as this is homework. But, you'll obviously need some way of operating on a piece of the list while keeping the rest of the list intact for the recursive part of you map function. As he said, Part is a good starting place, but I'd look at some of the other functions it references and see if they are more useful, like First and Rest. Also, I can see where Flatten would be useful. Finally, you'll need a way to end the recursion, so learning how to constrain patterns may be useful. Incidentally, this can be done in one or two lines depending on if you create a second definition for your map (the easier way), or not.

Hint: Now that you have your end condition, you need to answer three questions:

  1. how do I extract a single element from my list,
  2. how do I reference the remaining elements of the list, and
  3. how do I put it back together?

It helps to think of a single step in the process, and what do you need to accomplish in that step.

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