Mathematica Asked on January 2, 2022

```
comp1a = Flatten[Join[{{0 + 0 i}}, {comp1}]];
Flatten[comp1a[[1 ;; 6]]]
```

This creates a list of complex values -> comp1a

The first 6 terms in the list are:

{0, -2.04375 + 1.33694 I, -1.0492 + 3.3731 I, -4.95694 +

1.01694 I, 4.61625 – 4.52896 I, 6.66754 – 4.1488 I}

Then I try to pass the list to;

```
ExternalEvaluate["Python", "import numpy as np; timhist = np.fft.ifft(comp1a); timehist.tolist()"]
```

and the result is;

Failure

Message:"name ‘comp1a’ is not defined

Tag:PythonError

My question is how do I feed a list developed in Mathematica to a python function called externally

This will not work. Python does not know what `mylist`

is:

```
mylist = {1, 2, 3, 4};
ExternalEvaluate["Python", "sum(mylist)"]
```

You must get the value of `mylist`

into Python somehow. This could be done with string replacement:

```
ExternalEvaluate["Python", "sum(" <>
StringReplace[ToString[mylist], {"{" -> "[", "}" -> "]"}]
<> ")"]
```

... though as Jason .B noted in the comments, it's much simpler to write this as a `TemplateExpression`

:

```
ExternalEvaluate["Python", "sum(<* mylist *>)"]
```

For complex numbers, you'll need to rewrite them as a `complex`

type first before you can feed them to Python:

```
createcomplex[z_?NumericQ] :=
"complex(" <> ToString[Re[z]] <> "," <> ToString[Im[z]] <> ")"
createcomplex[-2.7 + 3.5 I]
(* result: "complex(-2.7,3.5)" *)
```

Or more simply, using templates:

```
toPyComplexStr[z_?NumericQ] :=
TemplateApply["complex(<* Re[z] *>, <* Im[z] *>)", z]
```

There is however, a better way in the documentation which doesn't depend on text substitution and it uses an external session:

```
session = StartExternalSession["Python"];
ExternalEvaluate[session, <|
"Command" -> "sum",
"Arguments" -> {NumericArray[{1 + I, 2.3 - I, 5.7 - 2.3 I, 7},
"ComplexReal32"]}
|>]
(* result: 16. - 2.3 I *)
```

Let's use it for your problem:

```
session = StartExternalSession["Python"];
result = [email protected]
ExternalEvaluate[
session, {"import numpy as np", <|"Command" -> "np.fft.ifft",
"Arguments" -> {NumericArray[{0, -2.04375 + 1.33694 I, -1.0492 +
3.3731 I, -4.95694 + 1.01694 I, 4.61625 - 4.52896 I,
6.66754 - 4.1488 I}, "ComplexReal32"]}|>}]
Normal[result]
(* result: {0.538983 - 0.491797 I, -1.01814 - 2.38259 I, -1.15996 + 0.0605042 I,
0.650033 + 0.10651 I, -1.85749 + 0.939762 I, 2.84658 + 1.76761 I} *)
```

... and notice that this result matches up with `InverseFourier`

if you use `FourierParameters -> {1, -1}`

:

```
InverseFourier[{0, -2.04375 + 1.33694 I, -1.0492 + 3.3731 I, -4.95694 + 1.01694 I, 4.61625 - 4.52896 I, 6.66754 - 4.1488 I},
FourierParameters -> {1, -1}]
(* result: {0.538983 - 0.491797 I, -1.01814 - 2.38259 I, -1.15996 + 0.0605042 I,
0.650033 + 0.10651 I, -1.85749 + 0.939762 I, 2.84658 + 1.76761 I} *)
```

Answered by flinty on January 2, 2022

1 Asked on December 7, 2020 by imas145

2 Asked on December 7, 2020 by hazoor-imran

0 Asked on December 7, 2020

equation solving linear algebra performance tuning probability or statistics

0 Asked on December 7, 2020

1 Asked on December 6, 2020

1 Asked on December 6, 2020 by karryma

1 Asked on December 6, 2020 by delivery101

1 Asked on December 6, 2020 by mathemania

calculus and analysis mathematical optimization numerical integration numerics plotting

0 Asked on December 6, 2020 by mahyar-s

1 Asked on December 6, 2020

differential equations equation solving mathematical optimization symbolic

1 Asked on December 5, 2020 by user13892

2 Asked on December 5, 2020 by xiaohuamao

0 Asked on December 5, 2020 by teo7

2 Asked on December 5, 2020

2 Asked on December 5, 2020 by kungfooman

1 Asked on December 4, 2020 by marc-kegel

Get help from others!

Recent Answers

- haakon.io on Why fry rice before boiling?
- Peter Machado on Why fry rice before boiling?
- Lex on Does Google Analytics track 404 page responses as valid page views?
- Joshua Engel on Why fry rice before boiling?
- Jon Church on Why fry rice before boiling?

Recent Questions

- How Do I Get The Ifruit App Off Of Gta 5 / Grand Theft Auto 5
- Iv’e designed a space elevator using a series of lasers. do you know anybody i could submit the designs too that could manufacture the concept and put it to use
- Need help finding a book. Female OP protagonist, magic
- Why is the WWF pending games (“Your turn”) area replaced w/ a column of “Bonus & Reward”gift boxes?
- Does Google Analytics track 404 page responses as valid page views?

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir