# speed up a code in mathematica.

**URL:** <https://community.wolfram.com/t/speed-up-a-code-in-mathematica/4104>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [November 24, 2014, 8:28pm UTC](https://community.wolfram.com/t/speed-up-a-code-in-mathematica/4104 "2014-11-24T20:28:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehsanfarsinia](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@ehsanfarsinia](https://community.wolfram.com/u/ehsanfarsinia)\
**Post date:** [November 24, 2014, 8:28pm UTC](https://community.wolfram.com/t/speed-up-a-code-in-mathematica/4104/1 "2014-11-24T20:28:09Z")

</div>

Professionals Hi,  
I wrote a program as follows. It takes a long time to answer. After two hours of running still does not answer. in the main program mm = nn = 30 that It takes so much more. Please help me. many thanks.

---

<div class="post-metadata">

**Author:** ![bmmmburns](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/bmmmburns/32/3126_2.png) [@bmmmburns](https://community.wolfram.com/u/bmmmburns)\
**Post date:** [November 24, 2014, 8:49pm UTC](https://community.wolfram.com/t/speed-up-a-code-in-mathematica/4104/2 "2014-11-24T20:49:55Z")

</div>

The final “diagonal” variable looks looks like it would cause an underflow if evaluated. (It has 1 + E^(1.5625\*10^22 (-…)) in the denominator.)

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [November 24, 2014, 9:08pm UTC](https://community.wolfram.com/t/speed-up-a-code-in-mathematica/4104/3 "2014-11-24T21:08:30Z")

</div>

A suggestion: that you work your way away from coding in a traditional procedural approach and recast your problems in more Mathematica-like functional, pattern-matching, Symbolic, and other paradigms. You will achieve much greater clarity and brevity, and you will learn the “way of Mathematica”.

---

<div class="post-metadata">

**Author:** ![ehsanfarsinia](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@ehsanfarsinia](https://community.wolfram.com/u/ehsanfarsinia)\
**Post date:** [November 25, 2014, 4:40am UTC](https://community.wolfram.com/t/speed-up-a-code-in-mathematica/4104/4 "2014-11-25T04:40:02Z")

</div>

@Marvin Burns  
i think main problem is in integrate part of code. how can i speed up integration in mathematica.

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [November 25, 2014, 4:58am UTC](https://community.wolfram.com/t/speed-up-a-code-in-mathematica/4104/5 "2014-11-25T04:58:24Z")

</div>

You should take a look at your various expressions along the way for a simplified version of your code: e.g. for

```
nn = 2;
mm = 2;

```

or even

```
nn = 1;
mm = 1;

```

Here is the value of your diagonal parameter in this latter case:

```
1/((-2 I + e) (1 + E^(
   1.5625\[CenterDot]10^22 (-(1/100000000000000000000) + e))) (2 I + 
   Conjugate[e]))

```

Ok, one sees that your integration variable was assumed complex along in your computation. Also see the huge numbers involved as well.

let’s “fix” that and see what we get:

```
diagonal /. Conjugate[e] -> e

```

gives

$\frac{1}{(e-2 i) (e+2 i) \left(e^{1.5625\times 10^{22}  
\left(e-\frac{1}{100000000000000000000}\right)}+1\right)}$

and

```
Integrate[diagonal /. Conjugate[e] -> e, {e, 0, Infinity}]

```

returns unevaluated, not surprisingly given that something like

```
Integrate[x^x, x]

```

returns unevaluated. So, the upshot of this is to perhaps take a line by line look at your algorithm to see what is happening at each stage and then see if there are things that can be done to make sure that it is doing what you are wanting. If your final result is expected to be a numerical result then once your code is revised you will probably want to explore NIntegrate

---

<div class="post-metadata">

**Author:** ![ehsanfarsinia](https://avatars.discourse-cdn.com/v4/letter/e/d07c76/32.png) [@ehsanfarsinia](https://community.wolfram.com/u/ehsanfarsinia)\
**Post date:** [November 25, 2014, 5:23am UTC](https://community.wolfram.com/t/speed-up-a-code-in-mathematica/4104/6 "2014-11-25T05:23:05Z")

</div>

yes my final result is numerical.NIntegrate is very fast but it`s answer is zero! it is not correct. where can i use NIntegrate?
