Message Boards Message Boards

0
|
14605 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Use function of a variable as a part specifier?

Posted 8 years ago

My question is how do I use a function of a variable such as PrimePi[x], Floor[x], or IntegerPart[x] as a part specifier to index the element of a list?

Assuming I have a single-dimensional list called data, I want to index into this list similar to one of the following:

data[[PrimePi[x]]]  
data[[Floor[x]]]  
data[[IntegerPart[x]]]  

When I try to do this, I get errors such as the following:

Part::pkspec1: The expression IntegerPart[x] cannot be used as a part specification.  

All of these functions evaluate to integers, so I don't understand why I can't index into a list with functions like these which I need to do for my application. I've tried assigning the function to a variable first, and then using the variable to index into the list and I get the same type of error.

The following example is only intended to illustrate the problem, and the function being computed in the example is not intended to have any practical value.

fOfY[y, x] := Sum[x/(n y), {n, 1, Floor[x]}]  

plotFOfY[fOfY[], xmax] := Block[{y = 1, i = 1, ylist = {}}, While[i <= xmax, If[PrimeQ[i], y *= i]; ylist = AppendTo[ylist, y]; i++]; Plot[fOfY[ylist[[PrimePi[x]]], x], {x, 2, xmax}, PlotRange -> Full, GridLines -> Automatic]]  

discretePlotFOfY[fOfY[], xmax] := Block[{y = 1, i = 1, ylist = {}}, While[i <= xmax, If[PrimeQ[i], y *= i]; ylist = AppendTo[ylist, y]; i++]; DiscretePlot[fOfY[ylist[[PrimePi[x]]], x], {x, 2, xmax}, PlotRange -> Full, GridLines -> Automatic]]  

When I run the discretePlotFOfY, I don't get any error messages.

discretePlotFOfY[fOfY[], 20]  

When I run the plotFOfY, I get error messages.

plotFOfY[fOfY[], 20]  



Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.
POSTED BY: Steven Clark
7 Replies

What exactly does not work. Please. post your code and result here

I am getting a result with no error

In[12]:= data = Table[RandomReal[{0, 100}], {10}]
data[[PrimePi[3]]]

Out[12]= {47.8615, 54.4348, 76.9031, 74.2042, 92.5634, 63.0498, \
12.3545, 43.1661, 29.4993, 69.0345}

Out[13]= 54.4348
POSTED BY: Jose Calderon

So I guess you need to define a function and then evaluate:

In[67]:= data

Out[67]= {73.284, 56.1426, 56.781, 11.0764, 75.6524, 21.7474, \
38.2588, 12.4285, 63.7419, 48.5597}

In[68]:= Clear[f, g]
(*simplest*)
f[x_] := data[[PrimePi[x]]]
(*with more generality*)
g[x_, function_: PrimePi, data_List: data] := 
 data[[function[x]]] /; 
  IntegerQ[function[x]] && function[x] >= 1 && 
   function[x] <= Length[data]

In[71]:= f[7]

Out[71]= 11.0764

In[72]:= g[7]

Out[72]= 11.0764

In[73]:= PrimePi[10]
PrimePi[100]

Out[73]= 4

Out[74]= 25

In[75]:= g[10, PrimePi, data]

Out[75]= 11.0764

In[76]:= g[100, PrimePi, data]

Out[76]= g[100, PrimePi, {73.284, 56.1426, 56.781, 11.0764, 75.6524, 
  21.7474, 38.2588, 12.4285, 63.7419, 48.5597}]
POSTED BY: Christian Neel
Posted 8 years ago

Christian: Thanks for the response, but not exactly. Assuming I have a single-dimensional list called data, I want to index into this list similar to one of the following:
data[[PrimePi[x]]]
data[[Floor[x]]]
data[[IntegerPart[x]]]

When I try to do this, I get errors such as the following:
Part::pkspec1: The expression IntegerPart[x] cannot be used as a part specification.

All of these functions evaluate to integers, so I don't understand why I can't index into a list with functions like these which I need to do for my application. I've tried assigning the function to a variable first, and then using the variable to index into the list and I get the same type of error.

POSTED BY: Steven Clark
Posted 8 years ago

What exactly does not work. Please. post your code and result here

I am getting a result with no error

In[12]:= data = Table[RandomReal[{0, 100}], {10}]
data[[PrimePi[3]]]

Out[12]= {47.8615, 54.4348, 76.9031, 74.2042, 92.5634, 63.0498, \
12.3545, 43.1661, 29.4993, 69.0345}

Out[13]= 54.4348
POSTED BY: Updating Name
Posted 8 years ago

Christian: Thanks for the response, but not exactly. Assuming I have a single-dimensional list called data, I want to index into this list similar to one of the following:
data[[PrimePi[x]]]
data[[Floor[x]]]
data[[IntegerPart[x]]]

When I try to do this, I get errors such as the following:
Part::pkspec1: The expression IntegerPart[x] cannot be used as a part specification.

All of these functions evaluate to integers, so I don't understand why I can't index into a list with functions like these which I need to do for my application. I've tried assigning the function to a variable first, and then using the variable to index into the list and I get the same type of error.

The following example is only intended to illustrate the problem, and the function being computed in the example is not intended to have any practical value.
fOfY[y, x] := Sum[x/(n y), {n, 1, Floor[x]}]

plotFOfY[fOfY[], xmax] := Block[{y = 1, i = 1, ylist = {}}, While[i <= xmax, If[PrimeQ[i], y *= i]; ylist = AppendTo[ylist, y]; i++]; Plot[fOfY[ylist[[PrimePi[x]]], x], {x, 2, xmax}, PlotRange -> Full, GridLines -> Automatic]]

discretePlotFOfY[fOfY[], xmax] := Block[{y = 1, i = 1, ylist = {}}, While[i <= xmax, If[PrimeQ[i], y *= i]; ylist = AppendTo[ylist, y]; i++]; DiscretePlot[fOfY[ylist[[PrimePi[x]]], x], {x, 2, xmax}, PlotRange -> Full, GridLines -> Automatic]]

When I run the discretePlotFOfY, I don't get any error messages.
discretePlotFOfY[fOfY[], 20]

When I run the plotFOfY, I get error messages.
plotFOfY[fOfY[], 20]

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

Part::pkspec1: The expression PrimePi[x] cannot be used as a part specification.

General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.

POSTED BY: Steven Clark

Something like that ?

In[1]:= data = Table[RandomReal[{0, 100}], {10}]

Out[1]= {73.284, 56.1426, 56.781, 11.0764, 75.6524, 21.7474, 38.2588, \
12.4285, 63.7419, 48.5597}

In[2]:= SortBy[data, Floor]

Out[2]= {11.0764, 12.4285, 21.7474, 38.2588, 48.5597, 56.1426, \
56.781, 63.7419, 73.284, 75.6524}
POSTED BY: Christian Neel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract