Linked Questions

1 vote
0 answers

Apply a test function to variable arguments [duplicate]

I want to provide a function f[x_,y___] which applies a pattern matching test also on the optional parameters (e.g. request that they are vectors of numerical quantities). In case of one single ...
Adalbert Hanßen's user avatar
2 votes
0 answers

Apply same type checking to every argument [duplicate]

Consider the following function which takes 10 arguments. f[Io_, {θ_, ϕ_}][{x0_, y0_, z0_}, d_][h_][L_][ω_] I know that all of these are of same type. I would ...
Anjan Kumar's user avatar
  • 4,999
602 votes
19 answers

Where can I find examples of good Mathematica programming practice?

I consider myself a pretty good Mathematica programmer, but I'm always looking out for ways to either improve my way of doing things in Mathematica, or to see if there's something nifty that I haven't ...
84 votes
4 answers

Using a PatternTest versus a Condition for pattern matching

My last question to the site resulted in several answers that involve using pattern matching in Mathematica, a feature I wasn't very familiar with at the time. I am currently reading Mathematica ...
Alfred Fazio's user avatar
  • 1,175
19 votes
5 answers

Implement the Bisection algorithm elegantly and easily

Description: Rencently, I have finished my course Numerical Analysis, so I'd like to implement many algorithm that I have learned from that course.By this practice, I hope that I can improve my ...
xyz's user avatar
  • 625
16 votes
4 answers

How to check the style and number of arguments like the built-in functions?

Given that I have a function sqrt[x] that I must check the style and number of argument x. I can do like this: ...
xyz's user avatar
  • 625
8 votes
2 answers

return {} when the function is given {}

I have a function taking an array as argument and giving an output. However, sometimes my argument is {} (the empty array), then how can I get an output {} when my argument is {}? Actually, I want the ...
lol's user avatar
  • 687
8 votes
4 answers

Define Function with Sum over a list

I want to define a function that would symbolically look like $$ t(s,\underline{a})=\pi s + \sum_{n=1}^{n_{max}}a_n\sin(n \pi s) $$ (something like a finite Fourier series). Here $s\in [0,1]$ and $\...
Malte's user avatar
  • 81
9 votes
4 answers

How to avoid repetitive calculation when doing numerical integral?

Suppose I have a function f[x] which is very complicated, together with a function g[f[x]]+h[x] to integrate. That is: ...
an offer can't refuse's user avatar
5 votes
1 answer

Why does pattern test not work in this case to restrict the function parameters?

I was looking at this Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across ...
seismatica's user avatar
  • 5,101
1 vote
1 answer

How can I define a function that takes a list of reals?

I'm trying to define a function f which takes a list of reals. My purpose is to build a function which I can give a list of reals, integers or strings and have it ...
Steven's user avatar
  • 11
1 vote
3 answers

Defining functions when there are a lot of parameters

Suppose I have two lists of parameters: $\delta=\{\delta1, \delta2, …\}$ and $\gamma = \{\gamma1, \gamma2, …\}$. The question is: how can one use $\delta$ and $\gamma$ (the parameter vectors) within ...
Seb's user avatar
  • 735
4 votes
1 answer

What is wrong with my code so it neither produces a correct plot nor an error message?

I am trying to define some initial functions and then using them to define my secondary functions and then plot the secondary functions. I am supposed to get two curves with a finite width ...
Benjamin's user avatar
  • 347
2 votes
1 answer

Generic templating system

I'm trying to implement a templating system in Mathematica, a bit like the List<List<T>> you would find in C#. Here's how it looks so far: ...
Literal's user avatar
  • 383