NumPy: Difference between revisions

From FreeCAD Documentation
(Wiki code.)
Line 10: Line 10:
<code>numpy</code> comes installed as a dependency of FreeCAD, therefore you can <code>import numpy as np</code> without having to first install it like in normal Python project environments.
<code>numpy</code> comes installed as a dependency of FreeCAD, therefore you can <code>import numpy as np</code> without having to first install it like in normal Python project environments.


== Convert Vector list between FreeCAD Python and NumPy ==


[https://forum.freecadweb.org/viewtopic.php?f=22&t=47529 Forum topic].
=== Convert App.Vector between FreeCAD Python and NumPy ===
* ''[https://forum.freecadweb.org/viewtopic.php?f=22&t=47529 source]''


=== From Python to NumPy ===


</translate>
* From Python to NumPy
{{Code|code=
: <source lang="Python">
import FreeCAD as App
import numpy as np
import numpy as np


vector_list = [App.Vector(1,0,0), App.Vector(1,2,3), App.Vector(0,3,0)]
vector_list = [App.Vector(1, 0, 0), App.Vector(1, 2, 3), App.Vector(0, 3, 0)]
numpy_array = np.asarray(vector_list)
numpy_array = np.asarray(vector_list)
print(numpy_array)
print(numpy_array)
}}
</source>
<translate>
:Output is

: <source>
Output is:

</translate>
{{Code|lang=text|code=
[[ 1. 0. 0.]
[[ 1. 0. 0.]
[ 1. 2. 3.]
[ 1. 2. 3.]
[ 0. 3. 0.]]
[ 0. 3. 0.]]
}}
</source>
<translate>


*From NumPy to Python
=== From NumPy to Python ===

: <source lang="Python">
</translate>
{{Code|code=
import FreeCAD as App
import numpy as np
import numpy as np


cad_list = [App.Vector(i) for i in numpy_array]
CAD_list = []
print(cad_list)
for i in numpy_array:
}}
CAD_list.append(App.Vector(i))
<translate>
print(CAD_list)
</source>
:Output is
: <source>
[Vector (1.0, 0.0, 0.0), Vector (1.0, 2.0, 3.0), Vector (0.0, 3.0, 0.0)]
</source>


Output is:
[[#top|top]]

</translate>
{{Code|lang=text|code=
[Vector (1.0, 0.0, 0.0), Vector (1.0, 2.0, 3.0), Vector (0.0, 3.0, 0.0)]
}}
<translate>


==FreeCAD Projects Using NumPy== <!--T:4-->
==FreeCAD Projects Using NumPy== <!--T:4-->

</translate>
</translate>
* https://github.com/looooo/freecad.gears/blob/master/setup.py#L13
* https://github.com/looooo/freecad.gears/blob/master/setup.py#L13
* https://github.com/booya-at/OpenGlider/blob/develop/setup.py#L77
* https://github.com/booya-at/OpenGlider/blob/develop/setup.py#L77
<translate>
<translate>

==FreeCAD Forum Discussion== <!--T:5-->
==FreeCAD Forum Discussion== <!--T:5-->


<!--T:6-->
<!--T:6-->
* [https://forum.freecadweb.org/viewtopic.php?f=22&t=47529 Any numpy experts?]
* [https://forum.freecadweb.org/viewtopic.php?f=22&t=47529 Any numpy experts?]



</translate>
</translate>

Revision as of 11:44, 9 September 2022

Other languages:

Introduction

This page aims to document how to use NumPy with FreeCAD.

numpy comes installed as a dependency of FreeCAD, therefore you can import numpy as np without having to first install it like in normal Python project environments.

Convert Vector list between FreeCAD Python and NumPy

Forum topic.

From Python to NumPy

import FreeCAD as App
import numpy as np

vector_list = [App.Vector(1, 0, 0), App.Vector(1, 2, 3), App.Vector(0, 3, 0)]
numpy_array = np.asarray(vector_list)
print(numpy_array)

Output is:

[[ 1.  0.  0.]
 [ 1.  2.  3.]
 [ 0.  3.  0.]]

From NumPy to Python

import FreeCAD as App
import numpy as np

cad_list = [App.Vector(i) for i in numpy_array]
print(cad_list)

Output is:

[Vector (1.0, 0.0, 0.0), Vector (1.0, 2.0, 3.0), Vector (0.0, 3.0, 0.0)]

FreeCAD Projects Using NumPy

FreeCAD Forum Discussion