NumPy: Difference between revisions

From FreeCAD Documentation
(Marked this version for translation)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
{{TOCright}}
<translate>
<translate>


Line 10: Line 11:
<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 == <!--T:7-->


=== Convert App.Vector between FreeCAD Python and NumPy ===
=== From Python to NumPy === <!--T:8-->

* From Python to NumPy
</translate>
: <source lang="Python">
{{Code|code=
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>
<!--T:9-->
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 === <!--T:10-->

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


cad_list = [App.Vector(itm) for itm in numpy_array]
CAD_list = []
print(cad_list)
for i in numpy_array:
}}
CAD_list.append(App.Vector(i))
<translate>
print(CAD_list)

</source>
<!--T:11-->
:Output is
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>
</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>

Latest revision as of 09:45, 11 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

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(itm) for itm 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