Hello,
So I'm trying to learn how to do things the functional way. As an exercise
I'm trying to program a simple economics related table that gives revenue,
marginal revenue, etc given a demand schedule (2 lists of numbers
representing quantity and price respectively). A simple task in a
spreadsheet.
The point is that I need a list of the differences in the given lists, from
which I will be using to get marginal values (derivatives). I'll call it,
say, DeltaList, and I was wondering if this would be a proper "functional"
way to go about the task:
In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
(DelatList: list -> list)
Basically, it partitions the list into ordered pairs, reverses those ordered
pairs to prep them for the subsequent Subtract application.
Is this the right way to think about this (painfully simple) problem in a
functional way?
Thank you. All of you have been most helpful in the past.
|
|
0
|
|
|
|
Reply
|
Just
|
1/20/2011 11:27:50 AM |
|
On Jan 20, 10:27 pm, Just A Stranger <forpeopleidontk...@gmail.com>
wrote:
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an exercise
> I'm trying to program a simple economics related table that gives revenue,
> marginal revenue, etc given a demand schedule (2 lists of numbers
> representing quantity and price respectively). A simple task in a
> spreadsheet.
>
> The point is that I need a list of the differences in the given lists, from
> which I will be using to get marginal values (derivatives). I'll call it,
> say, DeltaList, and I was wondering if this would be a proper "functional"
> way to go about the task:
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
> Basically, it partitions the list into ordered pairs, reverses those ordered
> pairs to prep them for the subsequent Subtract application.
>
> Is this the right way to think about this (painfully simple) problem in a
> functional way?
>
> Thank you. All of you have been most helpful in the past.
Check out Differences
??Differences
Shouldn't you also have a third axis for time? Revenue and quantity
are dynamic.
Mike
|
|
0
|
|
|
|
Reply
|
Armand
|
1/21/2011 9:31:27 AM
|
|
Of course that function would exist. Thank you for pointing that out. :)
On Thu, Jan 20, 2011 at 4:39 AM, Bob Hanlon <hanlonr@cox.net> wrote:
> DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> data = Array[x, 10];
>
> The simplest way is to just use Differences or ListCorrelate.
>
> DeltaList[data] ==
> Differences[data] ==
> ListCorrelate[{-1, 1}, data] ==
> Most[RotateLeft[data] - data] ==
> ({-1, 1}.# & /@ Partition[data, 2, 1])
>
> True
>
>
> Bob Hanlon
>
> ---- Just A Stranger <forpeopleidontknow@gmail.com> wrote:
>
> =============
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an exercise
> I'm trying to program a simple economics related table that gives revenue,
> marginal revenue, etc given a demand schedule (2 lists of numbers
> representing quantity and price respectively). A simple task in a
> spreadsheet.
>
> The point is that I need a list of the differences in the given lists, from
> which I will be using to get marginal values (derivatives). I'll call it,
> say, DeltaList, and I was wondering if this would be a proper "functional"
> way to go about the task:
>
>
>
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
>
> Basically, it partitions the list into ordered pairs, reverses those
> ordered
> pairs to prep them for the subsequent Subtract application.
>
> Is this the right way to think about this (painfully simple) problem in a
> functional way?
>
> Thank you. All of you have been most helpful in the past.
>
>
>
|
|
0
|
|
|
|
Reply
|
Just
|
1/21/2011 9:32:01 AM
|
|
On Thu, 20 Jan 2011, Just A Stranger wrote:
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an exercise
> I'm trying to program a simple economics related table that gives revenue,
> marginal revenue, etc given a demand schedule (2 lists of numbers
> representing quantity and price respectively). A simple task in a
> spreadsheet.
>
> The point is that I need a list of the differences in the given lists, from
> which I will be using to get marginal values (derivatives). I'll call it,
> say, DeltaList, and I was wondering if this would be a proper "functional"
> way to go about the task:
>
>
>
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
Hi
here is another way
data = RandomReal[{-1, 1}, 100];
DeltaList[data] = Most[RotateLeft[data] - data]
Oliver
>
> Basically, it partitions the list into ordered pairs, reverses those ordered
> pairs to prep them for the subsequent Subtract application.
>
> Is this the right way to think about this (painfully simple) problem in a
> functional way?
>
> Thank you. All of you have been most helpful in the past.
>
>
|
|
0
|
|
|
|
Reply
|
Oliver
|
1/21/2011 9:32:34 AM
|
|
It's a good start. You could also have tried DeltaList[L_] :=
Drop[RotateLeft[L] - L, -1] and probably countless others.
Of course, Mathematica already has a function that does this,
Differences.
Cheers -- Sjoerd
On Jan 20, 12:27 pm, Just A Stranger <forpeopleidontk...@gmail.com>
wrote:
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an exercise
> I'm trying to program a simple economics related table that gives revenue,
> marginal revenue, etc given a demand schedule (2 lists of numbers
> representing quantity and price respectively). A simple task in a
> spreadsheet.
>
> The point is that I need a list of the differences in the given lists, from
> which I will be using to get marginal values (derivatives). I'll call it,
> say, DeltaList, and I was wondering if this would be a proper "functional"
> way to go about the task:
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
> Basically, it partitions the list into ordered pairs, reverses those ordered
> pairs to prep them for the subsequent Subtract application.
>
> Is this the right way to think about this (painfully simple) problem in a
> functional way?
>
> Thank you. All of you have been most helpful in the past.
|
|
0
|
|
|
|
Reply
|
Sjoerd
|
1/21/2011 9:32:45 AM
|
|
On Jan 20, 3:27 am, Just A Stranger <forpeopleidontk...@gmail.com>
wrote:
> Hello,
>
> So I'm trying to learn how to do things the functional way. As an
> exercise I'm trying to program a simple economics related table
> that gives revenue, marginal revenue, etc given a demand schedule
> (2 lists of numbers representing quantity and price respectively).
> A simple task in a spreadsheet.
>
> The point is that I need a list of the differences in the given lists,
> from which I will be using to get marginal values (derivatives). I'll
> call it, say, DeltaList, and I was wondering if this would be a proper
> "functional" way to go about the task:
>
> In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
>
> (DelatList: list -> list)
>
> Basically, it partitions the list into ordered pairs, reverses those
> ordered pairs to prep them for the subsequent Subtract application.
>
> Is this the right way to think about this (painfully simple) problem
> in a functional way?
>
> Thank you. All of you have been most helpful in the past.
Instead of Reversing before subracting,
change all the signs after subtracting:
DeltaList2[L_] := - Subtract @@@ Partition[L, 2, 1]
Instead of partitioning, work with the entire vectors, lagged:
DeltaList3[L_] := Rest@L - Most@L
Or remember that there is already a built-in routine:
DeltaList4[L_] := Differences[L]
|
|
0
|
|
|
|
Reply
|
Ray
|
1/21/2011 9:33:07 AM
|
|
DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
data = Array[x, 10];
The simplest way is to just use Differences or ListCorrelate.
DeltaList[data] ==
Differences[data] ==
ListCorrelate[{-1, 1}, data] ==
Most[RotateLeft[data] - data] ==
({-1, 1}.# & /@ Partition[data, 2, 1])
True
Bob Hanlon
---- Just A Stranger <forpeopleidontknow@gmail.com> wrote:
=============
Hello,
So I'm trying to learn how to do things the functional way. As an exercise
I'm trying to program a simple economics related table that gives revenue,
marginal revenue, etc given a demand schedule (2 lists of numbers
representing quantity and price respectively). A simple task in a
spreadsheet.
The point is that I need a list of the differences in the given lists, from
which I will be using to get marginal values (derivatives). I'll call it,
say, DeltaList, and I was wondering if this would be a proper "functional"
way to go about the task:
In[0]: DeltaList[L_] := Subtract @@@ Reverse /@ Partition[L, 2, 1]
(DelatList: list -> list)
Basically, it partitions the list into ordered pairs, reverses those ordered
pairs to prep them for the subsequent Subtract application.
Is this the right way to think about this (painfully simple) problem in a
functional way?
Thank you. All of you have been most helpful in the past.
|
|
0
|
|
|
|
Reply
|
Bob
|
1/21/2011 9:35:09 AM
|
|
|
6 Replies
86 Views
(page loaded in 0.059 seconds)
|