如何使用数学中的功能编程减去列表中的特定元素? [英] How to subtract specific elements in a list using functional programming in Mathematica?

查看:152
本文介绍了如何使用数学中的功能编程减去列表中的特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的日期和值列表:

  {{{dateInfo1},value1},{{ dateInfo2},value2},...,{{dateInfoN},valueN}} 

日期和值:

  {{{1971,1,31,0,0,0.},1.0118},{ 1971,2,28,0,0,0},1.0075},
...,{{2010,5,31,0,0,0,0},1.0403}}

对于那些好奇的人,它是从 FRED数据库



我想简单地减去value1从值2,然后创建一个新的列表,其数据格式为:

  {{{dateInfo1},0},{ {dateInfo2},change1},...,{{dateInfoN},changeN-1}} 

(with change1 is value2-value1)



我知道使用功能编程来做相对比较容易的方法,而不是或者说wi指数变量和计数,所有这些废话。我想要完成的方法必须是相对强大的,因为我自动从具有相同格式,但不同时间间隔的源中提取数据集。如果我不必指定ListPlot日期间隔(如果我从列表中删除dateInfo将会发生),那么Replotting就会更容易。



我熟悉文档中心和非编程数学功能。我一直在使用Mathematica学习编程,并且真的希望把这种能力扩展到功能性编程中,但是发现这个主题的大部分资源有点太困难了。我觉得我正在学习曲线中的那个驼峰,即将要点击到位,但现在我正在努力。至少如果你有一个很好的功能编程来源,我会很高兴看看那些!任何帮助深表感谢!对不起,如果是TMI,但我相信你们中有很多人都有同样的感受。

解决方案

日期,价值}对,所以如果您移调您将有一个列表两个列表 - 第一个日期列表,第二个列表对应的值。
然后,您可以使用差异的值, Prepend 0,然后再转移到一个对列表中。 p>

在代码中,

  data = {{{1971,1,31 ,0,0,0},1.0118},
{{1971,2,28,0,0,0},1.0075},
{{2010,5,31,0,0,0 },1.0403}}
{dates,values} = Transpose [data];
diffs = Prepend [Differences [values],0];
answer =转置[{dates,diffs}]

返回:

  {{{1971,1,31,0,0,0},0},
{{1971,2,28 ,0,0,0},-0.0043},
{{2010,5,31,0,0,0},0.0328}}

将其包装成一个功能,感谢 Janus 对于这个想法:

  taildiffs [data _]:= 
Transpose @ {#1,Prepend [Differences [#2 ],0]}& @@ Transpose @ data

请注意, ...#1 .. 。#2 ...& 构造是一个纯函数:



http://reference.wolfram.com/mathematica/ref/Function.html



f @ x 语法简单地简写为 f [x]



最后, f @@ list 的缩写适用[f,list]



http:// reference。 wolfram.com/mathematica/ref/Apply.html



所以如上定义的taildiff只是一个简单(也许是隐含的)版本:

 应用[Transpose [Function [{x,y},{x,Prepend [Differences [y],0]}],Transpose [数据]] 


I have a list of dates and values in the format:

{{{dateInfo1},value1},{{dateInfo2},value2},...,{{dateInfoN},valueN}}

With some actual dates and values:

{{{1971, 1, 31, 0, 0, 0.}, 1.0118}, {{1971, 2, 28, 0, 0, 0}, 1.0075},
 ..., {{2010, 5, 31, 0, 0, 0.}, 1.0403}}

For those curious, it's a list of US versus CAD $ values pulled from the FRED database.

I would like to simply subtract value1 from value 2, and then create a new list with data in the form of:

 {{{dateInfo1},0},{{dateInfo2},change1},...,{{dateInfoN},changeN-1}}

(with change1 being value2-value1)

I know there must be a relatively easy way to do this using functional programming, as opposed to Do or While with index variables and counting and all that nonsense. The method I'm trying to accomplish has to be relatively robust, because I am automatically pulling in datasets from sources that have the same formatting, but different time intervals. Replotting is then much easier if I don't have to specify the ListPlot date intervals (which would happen if I stripped the dateInfo from the list).

I am familiar with the Documentation Center and non-programming Mathematica capabilities. I have been learning programming with Mathematica, and really want to extend that ability into functional programming, but have found most of the resources on the topic a bit too difficult. I feel like I am at that hump in the learning curve where it's about to click into place, but right now I am struggling. At the very least if you have a good source on functional programming I would be more than happy to look into those! Any help is much appreciated! Sorry if it's TMI, but I'm sure many of you have felt the same way.

解决方案

You have a list of {date,value} pairs so if you Transpose that you'll have a list of two lists -- the first a list of dates and the second a list of corresponding values. You can then take the Differences of the values, Prepend 0, and then Transpose again to get back to a list of pairs.

In code,

data = {{{1971,1,31,0,0,0}, 1.0118}, 
        {{1971,2,28,0,0,0}, 1.0075}, 
        {{2010,5,31,0,0,0}, 1.0403}}
{dates, values} = Transpose[data];
diffs = Prepend[Differences[values], 0];
answer = Transpose[{dates, diffs}]

which returns:

{{{1971,1,31,0,0,0}, 0}, 
 {{1971,2,28,0,0,0}, -0.0043}, 
 {{2010,5,31,0,0,0}, 0.0328}}

To wrap that up into a single function, with thanks to Janus for the idea:

taildiffs[data_]:= 
  Transpose @ {#1, Prepend[Differences[#2], 0]}& @@ Transpose@data  

Note that the ... #1 ... #2 ... & construct is a pure function:

http://reference.wolfram.com/mathematica/ref/Function.html

The f@x syntax is simply shorthand for f[x].

Finally, f@@list is shorthand for Apply[f, list]:

http://reference.wolfram.com/mathematica/ref/Apply.html

So taildiffs as defined above is just a terse (perhaps cryptic) version of this:

Apply[Transpose[Function[{x,y}, {x, Prepend[Differences[y],0]}], Transpose[data]]

这篇关于如何使用数学中的功能编程减去列表中的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆