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

3
votes
2answers
62 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 ...
6
votes
2answers
160 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 ...
6
votes
2answers
330 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} ...
0
votes
1answer
69 views

How can I write a function whose argument is a date, and whose output is the average temperature for the day? [on hold]

Consider I have some date, and I determine the average temperature for each day of the year. Using this, how can I write a function whose argument is a date, and whose output is the average ...
3
votes
5answers
230 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 ...
6
votes
1answer
83 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 ...
0
votes
0answers
24 views

Use WeatherData from many location [on hold]

Can anyone tell how can I predict the weather data in my location using weather data in other locations? Sort of algorithm, or equations??
-3
votes
0answers
67 views

Get the function of a scatter plot [closed]

Considering we have a scatter plot and the best linear fit of the data, and also average error of it, how can we get the function from that plot? The plot is I get it with ...
2
votes
1answer
205 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?
2
votes
1answer
165 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
85 views

Extract data from WeatherData as a list of associations

Given weather data for 2013 and 2014 as follows: ...
1
vote
2answers
212 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. ...
1
vote
1answer
53 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
72 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
142 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 ...
0
votes
0answers
75 views

How to make the evaluation of a function faster? [closed]

I have the code below. The ultimate function I am interested in is Mymodel[t]. ...
1
vote
1answer
26 views

Interpolation function evaluation and timing

Lets define two identical functions $f$ and $g$ in a slightly different way ...
1
vote
2answers
38 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
28 views

Why would this function work only when I make a copy of the variable? [duplicate]

Why would this function work with a copy of the function variable: ...
1
vote
1answer
42 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
120 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
79 views

Why is either Listable or HoldFirst not working?

Starting with a fresh kernel, I evaluate the following: ...
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
45 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
53 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: ...
3
votes
2answers
53 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 ...
0
votes
2answers
54 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
68 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 ...
4
votes
3answers
381 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
74 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 ...
9
votes
3answers
117 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 ...
1
vote
1answer
41 views

Overloading ClearAll[] to take no arguments

I use the command ClearAll["Global`*"] so much that I thought it would be nice to have a function that called it. Since by default ClearAll requires an ...
2
votes
1answer
49 views

Having variables appear aqua in user defined function [duplicate]

In many of the inbuilt mathematica function, variables appear aqua like in: However, when creating your own function, variables no longer have that green coloring and are instead colored like an ...
1
vote
1answer
36 views

Q-Multinomial Coefficient [closed]

How can one compute the q-Multinomial Coefficient as a function of q,m and a list {n1,n2,n3,...} in Mathematica? See http://mathworld.wolfram.com/q-MultinomialCoefficient.html for the definition.
4
votes
1answer
105 views

Multiple down-values with exactly the same lefthand side

I was working on an answer to this question, when I was sidetracked by what I bring up here. I wanted a function, which when given a string representing a binary number, would convert the string to a ...
3
votes
3answers
157 views

Function that prints a different value than it returns

I want to create a function something like this: test[x_] := (Print[1]; Return[2];) Except it prints 1 when executed as ...
0
votes
3answers
97 views

Using “With” outside a function on its parameters [duplicate]

I am making complex 3d graphics, and to clear up functions, I want to use With to remove a repeated action. This action occurs many times through many functions, so ...
1
vote
1answer
59 views

Why is Return not forcing an exit from my function? [duplicate]

Here is simplified version of my code that exhibits the behavior troubling me: ...
0
votes
1answer
35 views

Assigning a given value if a function returns an error [duplicate]

This question has to do with error handling, I guess. I am using Brent's Method in order to numerically find the root of a function. This is just an example of what I am doing: ...
2
votes
1answer
83 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 ...
1
vote
2answers
72 views

Returning ancillary computation results from a function

Suppose I want to implement a function to solve a very basic Elliptic problem using Finite Element of some degree (It's just an example). Normally the user of my function is only interested to the ...
0
votes
1answer
78 views

Defining a function that differentiates another function with rule replacement

This question is related to the question I posted here. In that question, I thought I have simplified my original problem into an equivalent concise version, but I found that it is not. So I decided ...
0
votes
0answers
57 views

How can I avoid a scoping problem when differentiating?

I simplify my real problem as follows: I define a rule function as Clear[rule]; rule[i_] := tt -> i; the real rule function is not this simple. I define a ...
3
votes
1answer
188 views

Defining the Moyal Product in Mathematica

What would be the best way to define the Moyal Product, $f(x,p)\star g(x,p)$, in Mathematica? $f(x,p)\star g(x,p)$ may be written as $f\star ...
13
votes
1answer
311 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 ...
3
votes
1answer
128 views

Gateaux (directional) derivatives and higher order differentials of a functional

I would like to calculate the Gateaux derivative of a functional (i.e. a function depending on functions). A simple example for the Dirichlet functional: $L(u(x))=\int_0^1 \frac{1}{2} (u'(x))^2 dx$ ...
0
votes
0answers
52 views

Maximum and mean of a multi-variate function over a restricted lattice

I have a function Q, and I want to find the maximum value, the corresponding arguments for the maximum value, and the mean over a certain range of arguments. I ...
1
vote
2answers
52 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 ...
3
votes
4answers
223 views

Defining a pure function involving a function argument

I've got an expression involving x and y that I want to use as a MeshFunction: ...
0
votes
1answer
48 views

Alternatives to indexed variables in Compile

According to the excellent answer given here by @Leonid Shifrin: How to compile effectively?, You can not really use indexed variables in Compile, although it ...