In Mathematica, I'd like to do something along the lines of:

```
f[Rational[a_, b_], Rational[c_, d_]] := {a+c, b+d}
```

But if I evaluate it with expressions of the following form I get the wrong result:

```
In: f[Rational[50, 100], Rational[4, 10]]
Out: {3, 7}
(* Expected: 54 / 110 -> 27 / 55 *)
```

Is there any way I can force Mathematica to stop simplifying the expression immediately? I can just do a hold on whatever I pass in, then have the function in question just call ReleaseHold[..] on whatever what was passed in.

This solution is very ugly though, and I don't want to have to do this. I know some functions in Mathematica automatically hold whatever is passed in and delay evaluating it for some reason or another, and I would like to do this here.

**In short: How can I force Mathematica to lazily evaluate something being passed into a function without having to manually hold it?**

In the standard evaluation procedure, each argument of a function is evaluated in turn. This is prevented by setting the attributes HoldFirst, HoldRest and HoldAll. These attributes make Mathematica "hold" particular arguments in an unevaluated form.

e.g.

`SetAttributes[yourFunction, HoldFirst]`

- http://reference.wolfram.com/mathematica/ref/HoldFirst.html

The docs say any auto-Held arguments are automatically evaluated the first time you use them in the function body. However if for some reason you want to continue working with the argument in the Hold form (e.g. if you'd like to do pattern-matching and rewriting on the unevaluated form of the expression), then perhaps you can re-`Hold`

it.

Using the `HoldAll`

attribute ninjagecko mentioned I was able to craft a solution.

There was actually another issue going on that I wasn't able to see immediately. Specifically, my function wasn't pattern matching as I thought it would be.

I thought my initial issue was simply that Mathematica was automatically simplifying my expressions and I needed to lazily evaluate the parameters being passed in for the correct behavior.

In reality, I forgot that there are multiple ways of representing expressions in Mathematica. As a toy example consider the following function which extracts the numerator and denominator of a fraction:

```
ExtractNumDem[Fraction[a_, b_]] := {a, b}
(* Already incorrect, ExtractNumDem[4 / 100] gives {1, 25} *)
```

Just adding the HoldAll (Or HoldFirst even) attribute results in another issue:

```
SetAttributess[ExtractNumDem, HoldAll];
ExtractNumDem[4 / 100] (* Gives ExtractNumDem[4 / 100] *)
```

The expression `4 / 100`

is actually evaluating to `Times[4, Power[100, -1]]`

. To fix this second issue I had to add a definition for fractions that look like that:

```
ExtractNumDem[Times[a_, Power[b_, -1]] := {a, b}
ExtractNumDem[4/100] (* Now gives {4, 100} *)
```

My solution to fixing the issue in my original answer applied the same exact principle. Here's some code to actually see the issue I was running into:

```
ClearAll[ExtractNumDem]
ExtractNumDem[Rational[a_, b_]] := {a, b}
ExtractNumDem[4 / 100]
SetAttributes[ExtractNumDem, HoldAll];
ExtractNumDem[4 / 100]
ExtractNumDem[Times[a_, Power[b_, -1]]] := {a, b}
ExtractNumDem[4/100]
```

Similar Questions

I'm attempting Mathematica programming, and thought I'd try a calculation that I did by hand manually (a magnetic field phasor calculation from an E&M class), using spherical polar coordinates. I'

(How) can I use dynamicHelpers with extend parameters like this? app.dynamicHelpers({ getImageUrl: function (req, res, imageName) { return http:// + req.headers.host + app.set(imageUrl)+/ + imag

I wish to plot a vector with the help of ListLinePlot function in mathematica. I want to change the scale of the Y axis in it. I cannot find a way in the documentation. The vector that I wish to plot

Hi guys i am looking for a way to add parameters on a print function, cause i have to print only the table and when i alert the table it shows me the correct value but when i am printing it prints the

Consider the following definition: f[x_]=Piecewise[{{0,x<1/2},{Interval[{0,1}],x==1/2},{1,x>1/2}}]; Then when one does the Plot[f[x],{x,0,1}] of the function, the graph does not depict the inter

I have a table with values under a column named:str_condition values in this column can be : variables.bit_male / application.bit_male / isdefined('session.int_user_id') The value can be complex as it

I reduced a debugging problem in Mathematica 8 to something similar to the following code: f = Function[x, list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5}; Count[list, x] ]; f[4] Maximize{f[x], x, Integers]

I am trying to plot a function in Mathematica that is defined over the unit simplex. To take a random example, suppose I want to plot sin(x1*x2*x3) over all x1, x2, x3 such that x1, x2, x3 >= 0 and

Mathematica provides MonomialList[f] to get all monomials in f: MonomialList[(x+y)^2]={x^2,2xy,y^2} But what I need is {x^2,xy,y^2}, how can I make it?

I am trying to simplify some formulas like F[a,b,c]*F[a,c,b]+F[c,a,b] /. {a->1,b->2,c->3} where F[a,b,c] := LegendreP[a,x]+LegendreP[b,x]*LegendreP[c,x] And Mathematica can give a polynomi

I'm trying to solve a problem: I need to translate an octave code in mathematica but I'm struggling with those lines. function g = NeumannBoundCond(f) [nrow,ncol] = size(f); g = f; g([1 nrow],[1 ncol]

I am beginner in Mathematica, and can only understand basic command so far. I need help in writing the maximum likelihood estimation function. I want to find the MLE of a Binomial distribution, with 2

I've created a macro variable active_search, which I'm trying to include in an evaluate function. However, the evaluate function is not outputting the correct value. Does anyone have suggestions on ho

Hi can anyone suggest me How can I call through app automatically I mean I used following code in my app. NSURL *url = [NSURL URLWithString:@telprompt://123-4567-890]; [[UIApplication sharedApplica

For example, if I have a function f(x)=x^2, how can I evaluate it at x=2? I have tried employing the symbolic toolbox and using the following code in the Command Window: syms x; f = sym(x^2); subs(f,x

Here's Mathematica's Nest function Definition. What's the eqv. in Ruby? The idea is this: nest(f, x, 3) #=> f(f(f(x)))

I'm doing some image processing in Mathematica (as homework). I'm working with this sort of list: { 1 -> {0.0125858, 0.068223, 0.000278799}, 2 -> {0.174, 0.304475, 0.126911}, 3 -> {0.0160498,

I have a function with many input parameters, and I need a function that will return a list of parameter names (not values) for each parameter whose value is '' or None Normally I'd throw an exception

I'm not really sure what to call these type of expressions, so an example would be easier... Is there a way in Perl to evaluate expressions like a < b <= c? I have a configuration file that allo

I want an arbitrary function p[x] that integrates to 1 and for all x, 0 <= p[x] <= 1. Some kind of transformation rule?

In our .Net application, some of our business objects use lazy loading to access data from the server. While debugging, if I want to inspect a property I have to be very careful and not look at or a

i want to understand in which case i should or have to use the evalute function. I have read the API doc about the evalute function of casperjs, but im unsure in which case i should use this function

Is it possible to pause Mathematica kernel during a computation? Here is an example. Module[{}, Mathematica code.... .......... .......... { Calls an external program with some argument Needs to wait

I have a function import to a SQL Proc which has several optional SQL parameters. CREATE PROC dbo.SomeProc ( @mandatoryField INT, @optionalField1 INT = NULL, @optionalField2 DATETIME = NULL ... ) How

I'm building a web scraper using spookyjs: https://github.com/WaterfallEngineering/SpookyJS I created a new spooky object and I'm trying to evaluate a function that returns the contents of a certain h

I have some calculations with an arbitrary function. In the output, Mathematica always shows the function arguments. I would like to tidy the notation a bit, by hidding the arguments in the output. Ho

I have f1[x_, y_] := x^2 - 10 x + y^2 + 8; f2[x_, y_] := x*y^2 + x - 10 y + 8; f[x_, y_] := {f1[x, y], f2[x, y]} ; x0 = {0, 0}; I want to evaluate f[x_, y_] in x0, so f[0, 0] I am doing this but does

Imagine I've defined a recursive factorial in Mathematica, like this: Clear[fact] fact[0] = 1 fact[n_] := n fact[n - 1] Evaluating fact[10] confirms that the function works and terminates. A bit of a

How can I get MSBuild to evaluate and print in a <Message /> task an absolute path given a relative path? Property Group <Source_Dir>..\..\..\Public\Server\</Source_Dir> <Program_

The mathematica documentation on Evaluate at possible issues says: Evaluate works only on the first level, directly inside a held function Why does Mathematica have this limitation? So if I have an

I tried to call boost::phoenix::function<> based on lambda function with parameters and failed. If I call it without parameters in such a way: auto closure = [](){ cout<<TEST; }; typedef

I'm relatively new to programming, have been working a lot in javascript, and now trying to learn php. The title basically says it. How many parameters can I pass in a user defined function? I've noti

I recently work on a project that heavily using collection filter function provided by Guava,but I found something like this won't produce the expected behavior: Predicate<ProductClassDTO> secL

Google is launching a new language, promising that it has a better performance, but how can I evaluate performance in Dart source code? Let's take as example the sun flower drawFrame method: // Dra

How can I pass parameters to the OnSuccess function of the AjaxOptions class in ASP.NET MVC? Here's my code but it doesn't work: <%= Ajax.ActionLink(Delete, Delete, MyController, New With {.i

I currently have this: get '/myapp/get/:func' do erb :server, :locals => {:func => params[:func]} end And then in my server.erb file I have this: if (func == myFunc1) myFunc1 elsif (func ==

With python how can I evaluate a dictionary of functions with their associate parameters. To be more specific, the keys are the functions and the values are a list of paramters. for example, consider

I am trying to import a histogram produced by Stata as an .eps file into Mathematica, but it does not display axes' labels. That is, for some reason, Mathematica does not import .eps as but rather tra

I have a variable that I need to pass to a subroutine. It is very possible that the subroutine will not need this variable, and providing the value for the variable is expensive. Is it possible to cre

I had tried the code below in Mathematica 8: f[z_] := (5 + 1/(z-a)) / ( 8 + 1/(z-a)) f[a] and surprisingly I got following warnings: Power::infy: Infinite expression 1/0 encountered. >> Power::

I can't seem to understand the difference between the behaviour of Plot and PlotLog (and other log-scale plotting functions) in Mathematica. Let's say I have this simple function: f [a_] := Length[Ran

When I create a web workers like the following... var w = new Worker(./Scripts/sample.js); sample.js want to some parameters from the caller!! Possible?

In MATLAB, how I can run 20 files .m (M-file) automatically?

How can I find the parameters of an undocumented Dll function? I have searched all over the internet and ended up finding one way: it involves decorated functions. However, I cannot find a way to get

Example: list:={ Plus[1,1], Times[2,3] } When looking at list, I get {2,6} I want to keep them unevaluated (as above) so that list returns { Plus[1,1], Times[2,3] } Later I want to evaluate the fun

I have a winforms TabControl and I am trying to cycle through all the controls contained in each tab. Is there a way to add and in a foreach loop or isn't it possible to evaluate more than one group o

I am trying to figure out, how can I automatically submit a url at the following page http://www.facebook.com/sharer.php without actually clicking on the submit button. Thanks.

I'm trying to evaluate a difficult sum: mathematica seems to evaluate it, giving the message Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corre

I have defined a controller in Sencha, that includes a refs attribute referencing my view, yet whenever I call the automatically generated get function to get the view, based on the refs attribute,

I was wondering if there is a macro or standard way (for debugging purposes) to automatically print the value of the parameters of a function f, just like __FUNCTION__ prints/shows the function signat