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
3answers
764 views

Make a differentiable smooth sawtooth waveform

So, I am trying to understand the effect of sawtooth waveform on a mechanical system model. For example, a system of a plate and a box on it. And then we try to oscillate the plate. In order to do ...
4
votes
1answer
168 views

Feedback on my Subscript code

I have written a code, and I need a little feed back.. I want to make the Subscript distributive across (1) addition and (2) multiplication to mimic short-hand for ...
3
votes
2answers
149 views

Multiplicative partition function

I am trying to create a multiplicative partition function that would generate something like ...
3
votes
3answers
214 views

Prime factor counting function

Is there any way I can speed up this prime factor counting function? (I am looking for all numbers in a range with 3 prime factors (counted with multiplicity).) ...
2
votes
1answer
157 views

Area between Contours in ContourPlot

I feel slightly foolish for asking this because I am so close, but I'm having trouble, so I will anyway. I asked this question two days ago regarding finding the lengths of contours. Now, I'd like to ...
2
votes
1answer
104 views

How to define a function of z[bar], etc

Using Displaying index as subscript on output: e.g. C[i] -> C_i with Notation[...] or Interpretation[..]? I have often ended up wanting to define functions of annotated variables to better match my ...
2
votes
1answer
774 views
2
votes
3answers
269 views

How do I construct a pure function to extract data fields from records that are strings?

How can I get from data segment format to the final segment format with a pure function? The data fields are fixed length. There are no separators between the fields. a) list ...
2
votes
3answers
2k views

Passing a matrix as a function argument

I am trying to do the following: ...
1
vote
3answers
176 views

professionalize/optimize my code that calls a function

I have the following construction, which defines a function that I subsequently call in a loop multiple times. I use this very often, but I have never looked into if there is a more professional way ...
1
vote
2answers
176 views

Function with different set of arguments [duplicate]

Possible Duplicate: How to create functions of arbitrary number of variables? i want to use a Function f[] with different set of arguments. example: ...
12
votes
4answers
368 views

Equivalent Nested Loop Structure

Consider the following examples: ...
11
votes
2answers
250 views

can a ColorFunction have side effects?

Can anyone explain this (to me) odd behavior. If I run this code once (fresh kernel) it works as intended. ...
9
votes
1answer
651 views

Functional programming and Do loops

I'm writing a Monte Carlo simulation. I'm using a Do loop to create the different simulations, iterating over different ...
8
votes
1answer
120 views

Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?

Consider the following expression: Block[{Print = CopyToClipboard}, doSomething[]] Now all calls to Print from ...
7
votes
1answer
146 views

Specifying optional arguments with variables

Perhaps this is poor programming on my part, but I would like to create a function that uses a variable as an optional parameter: var = 2; f[a_:var] := {a} The ...
7
votes
3answers
543 views

Composition of functions

I have a number of rotations computed by rot = RotationTransform[theta, point], and I would like to compose them to produce one function that is the composition of ...
6
votes
5answers
248 views

Find function inverse

I'm trying to find the inverse of a function: (30*x^2 (1 - x)^2) (* where 0<x<1 *) I tried all the following options: 1. ...
6
votes
3answers
169 views

Using PatternTest and/or Condition to detect equal list lengths as well as list element formats

On searching Mathematica.SE for information about overloading functions, I came across several threads about PatternTest and ...
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 ...
5
votes
2answers
159 views

How is OptionsPattern[] defined? Can I replicate this?

I am writing several functions which all take similar input parameters (a complicated nested list structure). Is there some way for me to define a pattern that can be used in multiple places and ...
4
votes
3answers
175 views

How to distribute a generic function of two arguments (without evaluating the arguments)

I guess there is an easy way to do this, but I can't seem to figure it out. What I would like to do is to distribute a generic (i.e. without specifying what this function really does) binary function. ...
4
votes
1answer
260 views

How do I use the result of Solve in a function definition?

I'm trying to adapt the following piece to the part where PowerF is a function of amax. ...
4
votes
2answers
180 views

Calling a function an unspecified number of times

I would like to be able to call a function an unspecified number of times. That is, I would like the generalization of something like: ...
3
votes
2answers
185 views

How to declare a function that uses local variables of a module outside this module?

I have the following code Module[{f,x=0}, f[t_] := x + t; ..........; ...f[1]...; ..........; ] My aim is to put the declaration of the function ...
3
votes
1answer
92 views

Use of StringExpression as argument

Is it possible to use a StringExpression as a pattern for an argument? I.E. something like foo[chars__~~ WordCharacter.. ~~ num:NumberString] := {chars, num}; At ...
3
votes
3answers
312 views

How to pull scalars out of a function that should act on lists?

Suppose I have A = a vecA B = b vecB where a and b are supposed to be arbitrary scalars ...
2
votes
1answer
50 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 ...
2
votes
2answers
154 views

Find partial derivatives when the number of variables is unknown

The idea is that I have a defined function of $n$ variables. I want to find out $n$ and get partial derivation for each of variables, Suppose you have a function ...
2
votes
2answers
326 views

Write a function that returns the logarithmic derivative

How can we write a function that if we input an expression f, it returns the log derivative $\frac{1}{f} \frac{df}{dx}$. We have to use a conditional or pattern test so that the function accepts any ...
2
votes
1answer
200 views

Can my permutations function be defined using iteration?

I have two functions, tuples and perm. They are similar. ...
2
votes
2answers
99 views
1
vote
2answers
168 views

Constraining function found by NDSolve to stay positive

I am trying to constrain my variables in NDSolve so that they stay positive for the integration interval. Is is possible to do that? Using Assumptions as in ...
1
vote
1answer
114 views

Module that finds the sum of digits of any given positive integer

How can I make a module that finds the sum of digits of any given positive integer? I use this command F[x_]:= Apply[ Plus, IntegerDigits[x] ] Is there another ...
1
vote
1answer
175 views

Part::pspec error returned when using a ListLinePlot ColorFunction variable as list index

In ListLinePlot, I'm attempting to construct my own ColorFunction for coloring points differently based on a third dimension of data not plotted to a specific axis. However, ColorFunction only uses ...
1
vote
2answers
135 views

How can define a set of nested parameters

I would like to know how I to define a function with a nested set of parameters (or whatever you would like to call it). For example, how would I get the following function ...
0
votes
1answer
79 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 ...
0
votes
0answers
58 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 ...
0
votes
0answers
66 views

How to insert a function into OptionsPattern?

I’ve written a function TableauForm to draw a standard Young Tableau. This problem is related to this one, however, the difficulty is that, a function has to be applied to the list in my case. This ...