For questions on writing functions (pure or using Set/SetDelayed) for any purpose, including the features that may be incorporated in those functions, such as options, patterns and conditions.

learn more… | top users | synonyms

4
votes
2answers
116 views

How to understand the process of ListCorrelate when it in two-dimensional condition?

Toady,I learn a function ListCorrelate,and I can understand process of execution his result in one-dimensional. However,the ...
2
votes
1answer
57 views

Using HoldFirst but not wishing to hold everything

I have a DynamicModule in which I go through some data analysis and keep or reject the results. Here is a toy example of what I am doing. ...
1
vote
1answer
36 views

Defining a function that takes a function as a parameter [on hold]

I have a function like this CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_] Now, I want to add a new parameter ...
1
vote
1answer
81 views

Function returned an unexpected list

I was trying out a question in Wellin's Programming with Mathematica that says the following. Question: Given a set of points in the plane (or 3-space), find the maximum distance between any pair of ...
4
votes
0answers
146 views

Strange behaviour of MMA in derivatives of some standard functions

There are some peculiar things to be discovered in derivatives of some standard functions in MMA: Strange behaviour Example 1: Abs We have ...
2
votes
0answers
48 views

How to make a new analytic function work seamlessly with Mathematica's analysis functions?

I am defining a new analytic function of six variables. I have algorithms to evaluate it numerically, and have amassed a large catalog of analytic properties. It is easy for me to program the ...
3
votes
0answers
53 views

Listable on first level

I have some function which take List as input. f[l_List]:=someActions[l] I need apply this function to a list of lists. Now I ...
0
votes
1answer
43 views

Output a symbol from Module or Block

I have troubles to output an entire symbol, a function in this case, from a module or a block. For instance myf = Block[{f}, f[3] = 33; f[4] = 4; f] Then I want ...
7
votes
2answers
159 views

Error computing sum of sum of digits

I've defined a function that computes the sum of the base-b digits of n: DigitSum[n_, b_] := Total[IntegerDigits[n, b]] Then I defined a function that computes ...
1
vote
1answer
51 views

PDF for Normal-Gamma Mixture

The normal-gamma mixture is: ...
9
votes
4answers
222 views

Recognizing special cases of a defined function for permuted arguments

I have a number of ugly ways to implement the following pattern recognition task, but I'm looking for something elegant to keep my notebook tidy. I need to define a function of six variables ...
2
votes
2answers
77 views

How to make functions from datasets

I have a csv that encodes the results of plugging certain parameters (say, A and B) into certain functions (say, ...
1
vote
1answer
37 views

Specifying independence of a variable in a function

I am dealing with a function of a finite number of variables and among the operations I wish to do is to differentiate it repeatedly with respect with any of the variables. Now this function happens ...
2
votes
1answer
78 views

Defining a rule with functions of arbitrary arguments

I have a set of "functions operators", say A1[u],A2[u],A3[u],and I would like to define a rule such that, for instance, (x*A1[u1]).(y*A2[u2]) returns ...
1
vote
1answer
81 views

Passing function as argument

I've been mucking around with passing functions as arguments to other functions and via trial and error seem to have something that works. I was wondering if someone could have a quick look at some ...
2
votes
1answer
42 views

How do I tweak the input assistant to use string completion on my own functions? [duplicate]

I would like to be able to use Mathematica's string completion feature for subvalues of my own functions. I tend to go for functions that follow the pattern ...
2
votes
2answers
80 views

Local variables whose definitions depend on each other [duplicate]

Consider the following (pseudo-)function definition: f[l_] := Let[{ a = Length[l], b = First[l], c = b/a, }, c*10] So for ...
18
votes
1answer
286 views

Kernel inexplicably hangs if I define a pure function without suppressing output

Save any unsaved work before trying as it may crash Mathematica and other programs by filling up the memory! In v9.0.1.0 on OSX 10.8.5, if I start a new session and define two functions ...
5
votes
3answers
208 views

difficulty abstracting the OptionValue pattern

I am building lots of functions that use Options and OptionValue and wanted to reduce the redundancy in the expressions that ...
3
votes
2answers
70 views

How to define a nestable Baseform function, BaseForm output becomes BaseForm input

Mathematica won't change a number's BaseForm recursively: 63969 // BaseForm[#, 16] & // BaseForm[#, 8] & // BaseForm[#, 2] & Well, I inspected former ...
7
votes
2answers
172 views

Account for powers of 0 and 1 when defining function

I have a function that I am defining as (assume m,n,a,b are non-negative integers): myfcn[x_^m_ p_^n_, x_^a_ p_^b_] := basefcn[m, n, a, b]; and then extending to ...
7
votes
2answers
343 views

Mapping data on Archimedes' spiral

I would like to map my data on Archimedes' spiral and preserve the distance between points on the curve. The test data consists of 20 evenly spaced {x,y} ...
6
votes
1answer
93 views

Functional-Style Partial Application and the Accumulator Pattern?

Following up on Functional-style Fixed-Length Queue Object?, I want to generalize it to a functional-style accumulator pattern. Lots of things look like accumulators, loosely defined as binary ...
3
votes
5answers
244 views

How to add periodicity to condition?

Is it possible to set the following function f[a], where a can be any real number? $$ f(\alpha) = \begin{cases} 1 ...
13
votes
1answer
327 views

Quick way to use conditioned patterns when defining multi-argument function?

I have a simple function that is supposed to only accept numeric values (i.e. complex/real numbers and constant symbols e.g. Pi, E). $$f(a,b,c)=a+b+c$$ Edit: I should have chosen a less simple ...
2
votes
1answer
88 views

Define PolyLog so that positive reals evaluate on upper edge of branch cut

Generalizing the previous question: Define Log so that negative reals evaluate on lower edge of branch For real positive values $x>1$, Mathematica's polylogarithm function ...
2
votes
1answer
173 views

Finding average error of the data

Considering I have some data, and I want to fit the data to a linear fit and find the average error from the linear fit, how can I do that using Mathematica? I have ...
2
votes
1answer
217 views

Make a scatter plot

Considering I have weather data for last 10 years for London, how can I make a scatter plot that will show the previous day's temperature as a function from today's temperature?
1
vote
2answers
216 views

Error with “Newton's method” in Wellin's Mathematica book

In Wellin's Programming with Mathematica book, here's one of his implementations of the Newton method, where the iteration runs until the error tolerance is reached. ...
2
votes
1answer
93 views

Extract data from WeatherData as a list of associations

Given weather data for 2013 and 2014 as follows: ...
1
vote
1answer
56 views

Remove curly brackets _completely_ to map a function to the elements of a list [duplicate]

Most list manipulations can be done with Flatten, Partition and Take. I didn't managed to ...
1
vote
1answer
73 views

How can I write a function which takes another function of a variable and the variable itself as arguments and gives a list of critical points?

This is an exercise from Power Programming with Mathematica by Wagner. I need a function which takes a function and a variable as arguments, like this: critpts[f_,x_]:= and then on the rhs I've got ...
5
votes
4answers
148 views

Defining a function with parameters as variables

I have an expression V = (a[1] + a[2])b[1]. How would I define a function of a[1], a[2] and ...
1
vote
1answer
28 views

Interpolation function evaluation and timing

Lets define two identical functions $f$ and $g$ in a slightly different way ...
1
vote
2answers
43 views

How to define a variable updating function? [duplicate]

I tried to program a Sudoku solver when I suddenly ran into this problem: I need a function which effectivley does this: ...
0
votes
1answer
29 views
17
votes
2answers
338 views

Pattern that matches colors

Suppose I'm writing a function that takes a color as a parameter; for example: drawShape[color_] := Graphics[{Style[Disk[], color]}]; But if the caller inputs an ...
1
vote
1answer
44 views

How to Solve a Sequence in Mathematica [duplicate]

Given the input and the output, how do you interpolate a function in Mathematica? For example if you know that $f(1)=\frac{1}{2}$, $f(2)=\frac{1}{3}$, $f(3)=\frac{1}{4}$,... how would you solve for ...
9
votes
1answer
141 views

How can I export my learned ClassiferFunction[] and PredictorFunction[]s?

Mathematica 10 provides beautiful high-level machine learning functionality. Sadly, the learned functions once created are rather opaque objects. I need to use them in other projects, so here is my ...
2
votes
1answer
94 views

Why is either Listable or HoldFirst not working?

Starting with a fresh kernel, I evaluate the following: ...
1
vote
2answers
55 views

Creating function with matrix argument

I am completely new to programming, so I apologize in advance for a stupid question. I want to program an algorithm for computing invariant factors of a finitely generated abelian group, so in ...
18
votes
2answers
1k views

Best practice of passing a large number of parameters to functions

I have a number of functions which all take a large number of parameters. I am wondering which is the best practice of passing these parameters to those functions. I could of course simply specify the ...
0
votes
2answers
47 views

Defining a function from a list [closed]

I have a list of 49 numbers, and I would like to define a function f such that f(n) is simply the nth number in the list. How can I do this?
3
votes
1answer
56 views

Manipulate inside a function definition slows down when passed a large data set

I'm working with large data sets (>1gb) which I'm visualizing with Manipulate[]. The following example works fine for me with no additional memory overhead: ...
0
votes
2answers
56 views

Eigenvectors inside a maniupulate function

I am trying to make a physics application and it requires that the eigenvectors be taken for a 3x3 matrix. It is my intention that Mathematica never have to deal with the matrices symbolically. ...
0
votes
1answer
78 views

multiple generators for iterative construction of fractal

The code below is an attempt to use more than one generator (in this case two) to generate a fractal using the standard iterative procedure involving generators. Only the first two stages of the ...
9
votes
3answers
119 views

Implement settable properties

I am looking for advice on implementing the following. (It is possible that the answer is that it is simply not a good idea to do this.) I would like to have an object similar to ...
5
votes
3answers
394 views

What is the right way to write a function that includes other functions?

I want to include multiple sub functions: $b(x) = 10x$ $w(x) = s + b(x)$ $c(x) = x^2$ into two main functions: $ua(x) = w(x)-c(x)$ $ub(x) = w(x)-c(x)^2$ As a reaction to this answer, I wrote ...
2
votes
2answers
75 views

Adding a tree to a list of trees within a loop [duplicate]

I'm trying to write a function which for a list of trees checks if a given tree is isomorphic with any of the listed, and if not - adds it to the list. I tried to use both ...