cutcutcodec 1.0.post3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cutcutcodec might be problematic. Click here for more details.
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/__init__.py +6 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/__main__.py +38 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/c_build.py +33 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/_helper_properties.py +52 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/audio/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/audio/properties/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/audio/properties/duration.py +83 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/ffprobe.py +637 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/graph/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/graph/find.py +82 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/graph/time_backprop.py +68 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/rate_audio.py +239 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/rate_video.py +97 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/shape.py +107 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/time_backprop.py +183 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/duration.py +162 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/nb_frames.py +159 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/rate.py +102 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/timestamps.py +194 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/container.py +40 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/encoder.py +152 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/filter.py +33 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/frame.py +127 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/frame_audio.py +205 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/frame_video.py +322 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/layout.py +206 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/meta_filter.py +41 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/muxer.py +184 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/node.py +164 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/stream.py +182 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/stream_audio.py +130 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/stream_video.py +193 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/__init__.py +7 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/ast_to_file.py +125 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/__init__.py +6 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/compatibility.py +734 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/default.py +99 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/encodec.py +40 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/muxer.py +8 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/rate.py +133 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/graph_to_ast.py +477 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/graph_to_json.py +104 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/graph_to_tree.py +190 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/json_to_graph.py +114 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/parse.py +123 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/__init__.py +15 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/dynamic.py +544 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/lambdify.py +612 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/preprocess.py +698 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/printer.py +926 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/tree_to_graph.py +168 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/decision/__init__.py +8 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/operation/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/operation/add.py +73 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/operation/remove.py +476 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/exceptions.py +60 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/add.py +170 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/cat.py +60 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/cut.py +112 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/delay.py +85 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/equation.py +261 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/fir.py +231 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/identity.py +48 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/resample.py +451 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/subclip.py +91 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/cut.py +74 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/mix/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/mix/audio_cast.py +208 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/mix/video_cast.py +197 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/subclip.py +53 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/add.py +331 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/cat.py +60 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/cut.py +113 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/delay.py +84 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/equation.py +265 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/identity.py +48 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/pad.py +99 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/subclip.py +91 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/audio/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/audio/empty.py +73 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/audio/equation.py +56 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/audio/noise.py +243 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/c_fractal.cpython-312-x86_64-linux-gnu.so +0 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/empty.py +69 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/equation.py +109 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/fractal/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/fractal/fractal.c +116 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/fractal/geometry.py +100 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/fractal/mandelbrot.py +176 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/noise.py +112 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/interfaces/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/interfaces/seedable.py +82 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/read_ffmpeg.py +904 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/read_svg.py +145 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/scheduler.py +356 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/write.py +340 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/basic.py +35 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/clean/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/clean/graph.py +80 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/hashes/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/hashes/graph.py +81 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/singleton.py +47 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cuda/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/factor/__init__.py +5 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/factor/proba.py +189 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/generalize/__init__.py +5 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/generalize/delay_to_fir.py +88 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/graph.py +17 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/parallel/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/parallel/buffer.py +90 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/utils.py +34 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/audio_5.1_narration.oga +0 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/intro.webm +0 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/logo.svg +13 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/project.json +226 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/video.mp4 +0 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/__init__.py +8 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/__main__.py +25 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/actions.py +63 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/app/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/app/app.py +248 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/base.py +50 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/container_input_ffmpeg.py +90 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_audio_delay.py +9 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_audio_equation.py +45 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_audio_fir.py +58 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_audio_subclip.py +9 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_cut.py +82 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_delay.py +20 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_subclip.py +21 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_video_delay.py +9 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_video_equation.py +53 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_video_subclip.py +9 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_audio_equation.py +14 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_audio_noise.py +19 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_video_equation.py +13 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_video_mandelbrot.py +60 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_video_noise.py +18 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/base.py +99 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/default.py +27 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/documentation.py +47 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/general.py +71 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/interface.py +305 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/loader.py +41 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/main.py +62 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edition_tabs.py +34 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry/base.py +122 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry/filters.py +42 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry/generators.py +42 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry_tabs.py +46 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/_helper.py +54 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/container.py +226 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/doc.py +67 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/encodec.py +289 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/main.py +173 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/flowchart/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/flowchart/edge_properties.py +56 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/flowchart/main.py +280 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/flowchart/node.py +44 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/main.py +151 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/menu.py +21 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/preferences/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/preferences/audio_settings.py +183 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/preferences/main.py +41 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/preferences/video_settings.py +257 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/run.py +71 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/main.py +30 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/slider.py +286 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/track.py +97 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/view.py +90 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/toolbar.py +29 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/tools.py +23 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/audio_player.py +103 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/control.py +96 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/frame_extractor.py +254 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/main.py +115 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/__init__.py +8 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/__main__.py +27 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/coding_style.py +49 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/generation.py +52 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/install.py +72 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/run.py +58 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/abstract.py +215 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/audio_cast.py +35 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/audio_equation.py +71 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/c_memory.py +132 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/compatibility.py +27 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/compile.py +42 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/encoder.py +23 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/filter_cut.py +107 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/filter_delay.py +71 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/filter_empty.py +40 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/fir.py +143 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/hashes.py +23 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/initialisation.py +24 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/muxer.py +22 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/nan_snapshot.py +39 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/read.py +192 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/serialization_equality.py +50 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/snapshot_shape.py +21 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/sympy_to_torch.py +135 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/thread_safe.py +77 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/time_range.py +55 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/video_equation.py +77 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/write.py +95 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/utils.py +40 -0
- cutcutcodec-1.0.post3.dist-info/LICENSE +7 -0
- cutcutcodec-1.0.post3.dist-info/METADATA +271 -0
- cutcutcodec-1.0.post3.dist-info/RECORD +233 -0
- cutcutcodec-1.0.post3.dist-info/WHEEL +5 -0
- cutcutcodec-1.0.post3.dist-info/entry_points.txt +6 -0
- cutcutcodec-1.0.post3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,926 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+
"""Static C compilation of atomic sympy expression.
|
|
4
|
+
|
|
5
|
+
This is enable to write a C code, compile it with gcc,
|
|
6
|
+
and import the file as a python function.
|
|
7
|
+
|
|
8
|
+
It is slowler to initialise than the dynamic version but it is faster to evaluate.
|
|
9
|
+
This static evaluation do not support broadcasting.
|
|
10
|
+
|
|
11
|
+
Implemented functions:
|
|
12
|
+
|
|
13
|
+
* sympy.Abs
|
|
14
|
+
* sympy.Add `+`
|
|
15
|
+
* sympy.atan
|
|
16
|
+
* sympy.cbrt
|
|
17
|
+
* sympy.cos
|
|
18
|
+
* sympy.exp
|
|
19
|
+
* sympy.Mul `*`
|
|
20
|
+
* sympy.Pow `/` and `**`
|
|
21
|
+
* sympy.sin
|
|
22
|
+
* sympy.sqrt
|
|
23
|
+
|
|
24
|
+
Not implemented functions:
|
|
25
|
+
|
|
26
|
+
* sympy.acos
|
|
27
|
+
* sympy.acosh
|
|
28
|
+
* sympy.Add `+` and `-`
|
|
29
|
+
* sympy.And
|
|
30
|
+
* sympy.arg
|
|
31
|
+
* sympy.asin
|
|
32
|
+
* sympy.asinh
|
|
33
|
+
* sympy.atan2
|
|
34
|
+
* sympy.atanh
|
|
35
|
+
* sympy.ceiling
|
|
36
|
+
* sympy.cosh
|
|
37
|
+
* sympy.Determinant
|
|
38
|
+
* sympy.Eq
|
|
39
|
+
* sympy.erf
|
|
40
|
+
* sympy.floor
|
|
41
|
+
* sympy.GreaterThan
|
|
42
|
+
* sympy.HadamardProduct
|
|
43
|
+
* sympy.im
|
|
44
|
+
* sympy.LessThan
|
|
45
|
+
* sympy.log
|
|
46
|
+
* sympy.loggamma
|
|
47
|
+
* sympy.MatAdd
|
|
48
|
+
* sympy.Max
|
|
49
|
+
* sympy.Min
|
|
50
|
+
* sympy.Mod `%`
|
|
51
|
+
* sympy.Ne
|
|
52
|
+
* sympy.Not
|
|
53
|
+
* sympy.Or
|
|
54
|
+
* sympy.re
|
|
55
|
+
* sympy.sign
|
|
56
|
+
* sympy.sinh
|
|
57
|
+
* sympy.StrictGreaterThan
|
|
58
|
+
* sympy.StrictLessThan
|
|
59
|
+
* sympy.tan
|
|
60
|
+
* sympy.tanh
|
|
61
|
+
* sympy.Trace
|
|
62
|
+
* sympy.Tuple
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
from fractions import Fraction
|
|
66
|
+
import collections
|
|
67
|
+
import re
|
|
68
|
+
import typing
|
|
69
|
+
|
|
70
|
+
from sympy.core.basic import Atom, Basic
|
|
71
|
+
from sympy.core.containers import Tuple
|
|
72
|
+
from sympy.core.numbers import Integer, One, Rational
|
|
73
|
+
from sympy.core.power import Pow
|
|
74
|
+
from sympy.core.relational import Equality
|
|
75
|
+
from sympy.core.symbol import Symbol
|
|
76
|
+
from sympy.functions.elementary.complexes import im as sympy_im, re as sympy_re
|
|
77
|
+
|
|
78
|
+
from cutcutcodec.core.exceptions import CompilationError
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
C_TYPES = (
|
|
82
|
+
"float", "double", "long double", "float complex", "double complex", "long double complex"
|
|
83
|
+
)
|
|
84
|
+
NP_TYPES = (
|
|
85
|
+
"NPY_FLOAT", "NPY_DOUBLE", "NPY_LONGDOUBLE", "NPY_CFLOAT", "NPY_CDOUBLE", "NPY_CLONGDOUBLE"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _printer(
|
|
90
|
+
tree: list[tuple[Symbol, typing.Union[Basic, None]]],
|
|
91
|
+
alloc: dict[Symbol, set[Symbol]],
|
|
92
|
+
args: set[Symbol],
|
|
93
|
+
) -> str:
|
|
94
|
+
"""Return the complete C source code of the module with the tree function numpy compatible.
|
|
95
|
+
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
tree : list[tuple[sympy.core.symbol.Symbol, typing.Union[sympy.core.basic.Basic, None]]]
|
|
99
|
+
Each steps.
|
|
100
|
+
alloc : dict[sympy.core.symbol.Symbol, set[sympy.core.symbol.Symbol]]
|
|
101
|
+
The intermediate variables to be declared and there respective dimensions.
|
|
102
|
+
args : set[sympy.core.symbol.Symbol]
|
|
103
|
+
All the inputs arguments required for this function.
|
|
104
|
+
|
|
105
|
+
Returns
|
|
106
|
+
-------
|
|
107
|
+
code : str
|
|
108
|
+
The complete C source code of the compilable python module.
|
|
109
|
+
The entry point is the ``lambdify`` function.
|
|
110
|
+
"""
|
|
111
|
+
out_symbs = list(tree[-1][1]) if isinstance(tree[-1][1], Tuple) else [tree[-1][0]]
|
|
112
|
+
kernel_args = args | set(out_symbs)
|
|
113
|
+
|
|
114
|
+
# frozen C / python environement
|
|
115
|
+
header = (
|
|
116
|
+
"#define PY_SSIZE_T_CLEA\n"
|
|
117
|
+
"#include <complex.h>\n"
|
|
118
|
+
"#include <math.h>\n"
|
|
119
|
+
"#include <numpy/arrayobject.h>\n"
|
|
120
|
+
"#include <omp.h>\n"
|
|
121
|
+
"#include <Python.h>\n"
|
|
122
|
+
"#include <stdio.h>\n"
|
|
123
|
+
"#include <sys/syscall.h>\n" # for thread check
|
|
124
|
+
"#include <unistd.h>\n" # for thread check
|
|
125
|
+
"#undef I"
|
|
126
|
+
)
|
|
127
|
+
python_context = ( # python module integration of the function
|
|
128
|
+
"static PyMethodDef lambdifyMethods[] = {\n"
|
|
129
|
+
' {"lambdify", py_lambdify, METH_VARARGS, "Function for sympy expr evaluation."},\n'
|
|
130
|
+
" {NULL, NULL, 0, NULL}\n"
|
|
131
|
+
"};\n"
|
|
132
|
+
"static struct PyModuleDef lambdify = {\n"
|
|
133
|
+
" PyModuleDef_HEAD_INIT,\n"
|
|
134
|
+
' "lambdify",\n'
|
|
135
|
+
' "Autogenerated sympy lambdify module.",\n'
|
|
136
|
+
" -1,\n"
|
|
137
|
+
" lambdifyMethods\n"
|
|
138
|
+
"};\n"
|
|
139
|
+
"PyMODINIT_FUNC PyInit_lambdify(void)\n"
|
|
140
|
+
"{\n"
|
|
141
|
+
" import_array();\n"
|
|
142
|
+
" return PyModule_Create(&lambdify);\n"
|
|
143
|
+
"};"
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
# kernel, define the ``c_lambdify`` functions
|
|
147
|
+
kernels: dict[str, set[str]] = {} # source code of each function and the context functions
|
|
148
|
+
errors = []
|
|
149
|
+
for c_type in C_TYPES:
|
|
150
|
+
try:
|
|
151
|
+
kernels[c_type] = _printer_kernel(
|
|
152
|
+
tree=tree,
|
|
153
|
+
alloc=set(alloc)-set(out_symbs),
|
|
154
|
+
args=kernel_args,
|
|
155
|
+
c_type=c_type,
|
|
156
|
+
)
|
|
157
|
+
except CompilationError as err:
|
|
158
|
+
errors.append(err)
|
|
159
|
+
if not kernels:
|
|
160
|
+
raise CompilationError(
|
|
161
|
+
"failed to compile the expression for all c types",
|
|
162
|
+
*(arg for err in errors for arg in err.args)
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
# kernel parser
|
|
166
|
+
parser = _printer_parser(
|
|
167
|
+
kernel_args=sorted(map(str, kernel_args)),
|
|
168
|
+
out_is_tuple=isinstance(tree[-1][1], Tuple),
|
|
169
|
+
args=args,
|
|
170
|
+
out_symbs=out_symbs,
|
|
171
|
+
valid_c_types=sorted(kernels),
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
# final assembly
|
|
175
|
+
return (
|
|
176
|
+
header + "\n\n"
|
|
177
|
+
+ "\n\n".join(sorted(func for funcs in kernels.values() for func in funcs)) + "\n\n"
|
|
178
|
+
+ parser + "\n\n"
|
|
179
|
+
+ python_context
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _printer_kernel(
|
|
184
|
+
tree: list[tuple[Symbol, typing.Union[Basic, None]]],
|
|
185
|
+
alloc: set[Symbol],
|
|
186
|
+
args: list[Symbol],
|
|
187
|
+
c_type: str,
|
|
188
|
+
) -> set[str]:
|
|
189
|
+
"""Return the source code of the C function's kernel.
|
|
190
|
+
|
|
191
|
+
Parameters
|
|
192
|
+
----------
|
|
193
|
+
tree : list[tuple[sympy.core.symbol.Symbol, typing.Union[sympy.core.basic.Basic, None]]]
|
|
194
|
+
Each steps.
|
|
195
|
+
alloc : set[sympy.core.symbol.Symbol]
|
|
196
|
+
The intermediate variables to be declared.
|
|
197
|
+
args : set[sympy.core.symbol.Symbol]
|
|
198
|
+
All the inputs arguments required for this function.
|
|
199
|
+
c_type : str
|
|
200
|
+
The C type of the numbers. Could be `float` or `double`.
|
|
201
|
+
|
|
202
|
+
Returns
|
|
203
|
+
-------
|
|
204
|
+
str
|
|
205
|
+
The C source code of the kernel function, no python context.
|
|
206
|
+
|
|
207
|
+
Examples
|
|
208
|
+
--------
|
|
209
|
+
>>> from pprint import pprint
|
|
210
|
+
>>> from sympy.abc import c, x
|
|
211
|
+
>>> from sympy import Number, Tuple, sin, symbols
|
|
212
|
+
>>> import numpy as np
|
|
213
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _printer_kernel
|
|
214
|
+
>>> _, _0, _1, _2, _3, _4, _5 = symbols("_ _:6")
|
|
215
|
+
>>> tree = [(_0, c**(-2)), (_1, 1/x), (_2, _0*_1), (_3, Number(0)), (_1, sin(x)), (_1, sin(_1)),
|
|
216
|
+
... (_1, _1 + 1), (_0, c), (_4, x), (_5, _2), (_, Tuple(_3, _0, c, _4, x, _2, _5, _1))]
|
|
217
|
+
>>> alloc = {_0: {c}, _1: {c}, _2: {c}, _3: {c}, _4: {c}, _5: {c}}
|
|
218
|
+
>>> kernel = _printer_kernel(tree, alloc, [_2, _3, c, _4, x, _0, _5, _1], "float")
|
|
219
|
+
>>> for k in sorted(kernel): # doctest: +ELLIPSIS
|
|
220
|
+
... print(k)
|
|
221
|
+
...
|
|
222
|
+
void lambdify_float(const npy_intp _dim, float *_0, float *_1, ..., float *x) {
|
|
223
|
+
npy_intp _i;
|
|
224
|
+
#pragma omp parallel for simd schedule(static)
|
|
225
|
+
for ( _i = 0; _i < _dim; ++_i ) {
|
|
226
|
+
float _0, _1, _2, _3, _4, _5;
|
|
227
|
+
_0[_i] = 1.0f / c[_i] * c[_i];
|
|
228
|
+
_1[_i] = 1.0f / x[_i];
|
|
229
|
+
_2[_i] = _0[_i] * _1[_i];
|
|
230
|
+
_3[_i] = 0.0f;
|
|
231
|
+
_1[_i] = sinf(x[_i]);
|
|
232
|
+
_1[_i] = sinf(_1[_i]);
|
|
233
|
+
_1[_i] += 1.0f;
|
|
234
|
+
_0[_i] = c[_i];
|
|
235
|
+
_4[_i] = x[_i];
|
|
236
|
+
_5[_i] = _2[_i];
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
>>>
|
|
240
|
+
"""
|
|
241
|
+
context = set()
|
|
242
|
+
code_lines = []
|
|
243
|
+
|
|
244
|
+
# signature
|
|
245
|
+
args_ = [
|
|
246
|
+
"const npy_intp _dim",
|
|
247
|
+
*(f"{c_type} *{a}" for a in sorted(map(str, args))),
|
|
248
|
+
]
|
|
249
|
+
code_lines.append(f"void lambdify_{c_type.replace(' ', '_')}({', '.join(args_)})" + " {")
|
|
250
|
+
|
|
251
|
+
# global declaration
|
|
252
|
+
code_lines.append(" npy_intp _i;")
|
|
253
|
+
|
|
254
|
+
# main for declaration
|
|
255
|
+
code_lines.append(" #pragma omp parallel for simd schedule(static)")
|
|
256
|
+
code_lines.append(" for ( _i = 0; _i < _dim; ++_i ) {")
|
|
257
|
+
|
|
258
|
+
# expression on one item
|
|
259
|
+
kernel_lines = []
|
|
260
|
+
indexing = collections.defaultdict(lambda: "", {a: "[_i]" for a in args})
|
|
261
|
+
for out, expr in (tree[:-1] if isinstance(tree[-1][1], Tuple) else tree):
|
|
262
|
+
new_context, new_alloc, new_lines = _print_atomic(expr, out, indexing, c_type)
|
|
263
|
+
context |= new_context
|
|
264
|
+
alloc |= new_alloc
|
|
265
|
+
kernel_lines.extend([f" {line}" for line in new_lines])
|
|
266
|
+
if alloc:
|
|
267
|
+
code_lines.append(f" {c_type} {', '.join(sorted(map(str, alloc)))};")
|
|
268
|
+
code_lines.extend(kernel_lines)
|
|
269
|
+
code_lines.append(" }") # close for
|
|
270
|
+
|
|
271
|
+
# close main function
|
|
272
|
+
code_lines.append("}")
|
|
273
|
+
return context | {"\n".join(code_lines)}
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def _printer_parser(
|
|
277
|
+
kernel_args: list[str],
|
|
278
|
+
out_is_tuple: bool,
|
|
279
|
+
args: set[Symbol],
|
|
280
|
+
out_symbs: list[Symbol],
|
|
281
|
+
valid_c_types: list[str],
|
|
282
|
+
) -> str:
|
|
283
|
+
"""Write the code of the C python parser.
|
|
284
|
+
|
|
285
|
+
Help to ``cutcutcodec.core.sympy_to_torch.printer._printer``.
|
|
286
|
+
|
|
287
|
+
Parameters
|
|
288
|
+
----------
|
|
289
|
+
kernel_args : list[str]
|
|
290
|
+
The ordered list of the input symbol name of the kernels functions.
|
|
291
|
+
out_is_tuple : boolean
|
|
292
|
+
True if the returned value has to be packed inside a tuple.
|
|
293
|
+
args : set[sympy.core.symbol.Symbol]
|
|
294
|
+
All the inputs arguments required for this function.
|
|
295
|
+
out_symbs : list[sympy.core.symbol.Symbol]
|
|
296
|
+
The ordered list of the symbols to return.
|
|
297
|
+
valid_c_types : list[str]
|
|
298
|
+
The c type list where the kernel compilation is successful
|
|
299
|
+
"""
|
|
300
|
+
ref_symb = min(args, key=str)
|
|
301
|
+
|
|
302
|
+
# function api and variables declaration
|
|
303
|
+
parser = ""
|
|
304
|
+
parser += "static PyObject *py_lambdify(PyObject *self, PyObject *args) {\n"
|
|
305
|
+
parser += " PyArrayObject " + ", ".join(f"*{a}" for a in sorted(map(str, kernel_args))) + ";\n"
|
|
306
|
+
parser += " npy_intp _dim;\n"
|
|
307
|
+
if out_is_tuple:
|
|
308
|
+
parser += " PyObject *_out;\n"
|
|
309
|
+
|
|
310
|
+
# parse the input arguments
|
|
311
|
+
args_ = [
|
|
312
|
+
"args",
|
|
313
|
+
('"' + "O!"*len(args) + '"'),
|
|
314
|
+
*(f"&PyArray_Type, &{a}" for a in sorted(map(str, args)))
|
|
315
|
+
]
|
|
316
|
+
parser += f" if ( !PyArg_ParseTuple({', '.join(args_)}) )" + "{\n"
|
|
317
|
+
parser += " return NULL;\n"
|
|
318
|
+
parser += " }\n"
|
|
319
|
+
parser += f" _dim = PyArray_DIM({ref_symb}, 0);\n"
|
|
320
|
+
|
|
321
|
+
# allocation of the new tensors
|
|
322
|
+
args_ = sorted(map(str, set(out_symbs)-args))
|
|
323
|
+
for i, new in enumerate(args_):
|
|
324
|
+
parser += (
|
|
325
|
+
f" {new} = (PyArrayObject *)PyArray_SimpleNew(1, &_dim, PyArray_TYPE({ref_symb}));\n"
|
|
326
|
+
)
|
|
327
|
+
parser += f" if ( NULL == {new} ) " + "{\n"
|
|
328
|
+
parser += (
|
|
329
|
+
f' PyErr_SetString(PyExc_RuntimeError, "failed to create a new {new} array");\n'
|
|
330
|
+
)
|
|
331
|
+
for j in range(i):
|
|
332
|
+
parser += f" Py_DECREF({args_[j]});\n"
|
|
333
|
+
parser += " return NULL;\n"
|
|
334
|
+
parser += " }\n"
|
|
335
|
+
|
|
336
|
+
# increment ref of out tensors
|
|
337
|
+
for out_ in sorted(set(out_symbs) & args, key=str):
|
|
338
|
+
parser += f" Py_INCREF((PyObject *){out_});\n"
|
|
339
|
+
|
|
340
|
+
# thread management
|
|
341
|
+
parser += " if (syscall(SYS_gettid) == getpid()) {\n"
|
|
342
|
+
parser += " omp_set_num_threads(omp_get_num_procs());\n"
|
|
343
|
+
parser += " }\n"
|
|
344
|
+
parser += " else {\n"
|
|
345
|
+
parser += " omp_set_num_threads(1);\n"
|
|
346
|
+
parser += " }\n"
|
|
347
|
+
|
|
348
|
+
# call the C func
|
|
349
|
+
parser += f" switch(PyArray_TYPE({ref_symb})) " + "{\n"
|
|
350
|
+
for c_type in valid_c_types:
|
|
351
|
+
parser += f" case {NP_TYPES[C_TYPES.index(c_type)]}:\n"
|
|
352
|
+
parser += " Py_BEGIN_ALLOW_THREADS\n"
|
|
353
|
+
args_ = [
|
|
354
|
+
"_dim",
|
|
355
|
+
*(f"({c_type} *)PyArray_DATA({a})" for a in sorted(map(str, kernel_args))),
|
|
356
|
+
]
|
|
357
|
+
parser += f" lambdify_{c_type.replace(' ', '_')}({', '.join(args_)});\n"
|
|
358
|
+
parser += " Py_END_ALLOW_THREADS\n" # unlock the GIL, allow thread
|
|
359
|
+
if out_is_tuple:
|
|
360
|
+
args_ = [
|
|
361
|
+
str(len(out_symbs)),
|
|
362
|
+
*(f"(PyObject *){o}" for o in out_symbs),
|
|
363
|
+
]
|
|
364
|
+
parser += f" _out = PyTuple_Pack({', '.join(args_)});\n"
|
|
365
|
+
for out_ in out_symbs:
|
|
366
|
+
parser += f" Py_DECREF({out_});\n"
|
|
367
|
+
parser += " return _out;\n"
|
|
368
|
+
else:
|
|
369
|
+
parser += f" return (PyObject *){out_symbs[0]};\n"
|
|
370
|
+
parser += " default:\n"
|
|
371
|
+
parser += ' PyErr_SetString(PyExc_TypeError, "the array type is not supported");\n'
|
|
372
|
+
for out_ in out_symbs:
|
|
373
|
+
parser += f" Py_DECREF({out_});\n"
|
|
374
|
+
parser += " return NULL;\n"
|
|
375
|
+
parser += " }\n"
|
|
376
|
+
parser += "}"
|
|
377
|
+
|
|
378
|
+
return parser
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def _atom2str(elem: Atom, indexing: collections.defaultdict[Symbol, str], c_type: str) -> str:
|
|
382
|
+
"""Help for `_print_atomic`."""
|
|
383
|
+
if elem.is_symbol:
|
|
384
|
+
return f"{elem}{indexing[elem]}"
|
|
385
|
+
if elem.is_number:
|
|
386
|
+
if c_type in {"float", "double", "long double"}:
|
|
387
|
+
if elem.is_real or elem.is_integer:
|
|
388
|
+
suffix = {"float": "f", "double": "", "long double": "L"}[c_type]
|
|
389
|
+
return f"{float(elem)}{suffix}"
|
|
390
|
+
raise CompilationError(f"impossible to compile {elem} as C type {c_type}")
|
|
391
|
+
if c_type in {"float complex", "double complex", "long double complex"} and elem.is_complex:
|
|
392
|
+
suffix = {
|
|
393
|
+
"float complex": "f", "double complex": "", "long double complex": "L"
|
|
394
|
+
}[c_type]
|
|
395
|
+
return (
|
|
396
|
+
f"({float(sympy_re(elem))}{suffix} + {float(sympy_im(elem))}{suffix} * _Complex_I)"
|
|
397
|
+
)
|
|
398
|
+
raise CompilationError(f"failed to compile {elem} in C")
|
|
399
|
+
raise CompilationError(f"{elem} should be atomic but is not")
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def _print_atomic(
|
|
403
|
+
expr: Basic, out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str
|
|
404
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
405
|
+
"""Write the sympy atomic expression as a valid C source code lines.
|
|
406
|
+
|
|
407
|
+
Parameters
|
|
408
|
+
----------
|
|
409
|
+
expr : sympy.core.basic.Basic
|
|
410
|
+
The sympy atomic expression to eval.
|
|
411
|
+
out : sympy.core.symbol.Symbol
|
|
412
|
+
The variable set, assume that this var is already declared.
|
|
413
|
+
indexing : collections.defaultdict[sympy.core.symbol.Symbol, str]
|
|
414
|
+
The way to acces to the content of the vars, at the current dimension.
|
|
415
|
+
An empty string mean than the var is directely accessible with no indexingation.
|
|
416
|
+
Otherwise, the value corresponds to the content of the braquets.
|
|
417
|
+
For example, "[_i]" means ``var[_i]`` and "[_j - 2]" means ``var[_j - 2]``.
|
|
418
|
+
c_type : str
|
|
419
|
+
The C data type of the variables.
|
|
420
|
+
|
|
421
|
+
Returns
|
|
422
|
+
-------
|
|
423
|
+
context : set[str]
|
|
424
|
+
The functions requiered for the returned code lines.
|
|
425
|
+
alloc : set[sympy.core.symbol.Symbol]
|
|
426
|
+
The variables witch need to be declared before this code lines
|
|
427
|
+
code_lines : list[str]
|
|
428
|
+
The C code lignes corresponding to the given expression.
|
|
429
|
+
|
|
430
|
+
Raises
|
|
431
|
+
------
|
|
432
|
+
cutcutcodec.core.exceptions.CompilationError
|
|
433
|
+
If no printer is available for this expression.
|
|
434
|
+
"""
|
|
435
|
+
if expr.is_Atom:
|
|
436
|
+
return (
|
|
437
|
+
set(),
|
|
438
|
+
set(),
|
|
439
|
+
[f"{_atom2str(out, indexing, c_type)} = {_atom2str(expr, indexing, c_type)};"],
|
|
440
|
+
)
|
|
441
|
+
try:
|
|
442
|
+
func = globals()[f"_{expr.__class__.__name__.lower()}"]
|
|
443
|
+
except KeyError as err:
|
|
444
|
+
raise CompilationError(
|
|
445
|
+
f"no function {expr.__class__.__name__} for {expr} in {c_type}"
|
|
446
|
+
) from err
|
|
447
|
+
return func(out, indexing, c_type, *expr.args)
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def _abs(
|
|
451
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, arg: Atom
|
|
452
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
453
|
+
"""C abs operation.
|
|
454
|
+
|
|
455
|
+
Examples
|
|
456
|
+
--------
|
|
457
|
+
>>> from sympy.abc import x
|
|
458
|
+
>>> from sympy.functions.elementary.complexes import Abs
|
|
459
|
+
>>> import numpy as np
|
|
460
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _lambdify_c
|
|
461
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic, _printer
|
|
462
|
+
>>> func = _lambdify_c(_printer([(x, Abs(x))], {}, {x}))
|
|
463
|
+
>>> func(np.array([np.nan, -np.inf, -2., 2., np.inf]))
|
|
464
|
+
array([nan, inf, 2., 2., inf])
|
|
465
|
+
>>> func(np.array([3+4j, -3+4j, 3-4j, -3-4j]))
|
|
466
|
+
array([5.+0.j, 5.+0.j, 5.+0.j, 5.+0.j])
|
|
467
|
+
>>>
|
|
468
|
+
"""
|
|
469
|
+
cfunc = {
|
|
470
|
+
"float": "fabsf",
|
|
471
|
+
"double": "fabs",
|
|
472
|
+
"long double": "fabsl",
|
|
473
|
+
"float complex": "(float complex)fabsf",
|
|
474
|
+
"double complex": "(double complex)cabs",
|
|
475
|
+
"long double complex": "(long double complex)fabsl",
|
|
476
|
+
}[c_type]
|
|
477
|
+
code = f"{_atom2str(out, indexing, c_type)} = {cfunc}({_atom2str(arg, indexing, c_type)});"
|
|
478
|
+
return set(), set(), [code]
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def _add(
|
|
482
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, *parts: Atom
|
|
483
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
484
|
+
"""C + operation.
|
|
485
|
+
|
|
486
|
+
Examples
|
|
487
|
+
--------
|
|
488
|
+
>>> from collections import defaultdict
|
|
489
|
+
>>> from sympy.abc import x, y, z
|
|
490
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic
|
|
491
|
+
>>> _print_atomic(1 + x, x, defaultdict(lambda: ""), "float")
|
|
492
|
+
(set(), set(), ['x += 1.0f;'])
|
|
493
|
+
>>> _print_atomic(1 + x, y, defaultdict(lambda: ""), "float")
|
|
494
|
+
(set(), set(), ['y = 1.0f + x;'])
|
|
495
|
+
>>> _print_atomic(1 + x + y, z, defaultdict(lambda: ""), "float")
|
|
496
|
+
(set(), set(), ['z = 1.0f + x;', 'z += y;'])
|
|
497
|
+
>>>
|
|
498
|
+
"""
|
|
499
|
+
out_str = _atom2str(out, indexing, c_type)
|
|
500
|
+
if len(parts) == 0:
|
|
501
|
+
parts = (Integer(0),)
|
|
502
|
+
if len(parts) == 1:
|
|
503
|
+
return set(), set(), [f"{out_str} = {_atom2str(parts[0], indexing, c_type)};"]
|
|
504
|
+
try:
|
|
505
|
+
ind = parts.index(out)
|
|
506
|
+
except ValueError:
|
|
507
|
+
code_lines = [
|
|
508
|
+
f"{out_str} = "
|
|
509
|
+
f"{_atom2str(parts[0], indexing, c_type)} + {_atom2str(parts[1], indexing, c_type)};"
|
|
510
|
+
]
|
|
511
|
+
parts = parts[2:]
|
|
512
|
+
else:
|
|
513
|
+
code_lines = []
|
|
514
|
+
parts = tuple(p for i, p in enumerate(parts) if i != ind)
|
|
515
|
+
code_lines.extend([f"{out_str} += {_atom2str(p, indexing, c_type)};" for p in parts])
|
|
516
|
+
return set(), set(), code_lines
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
def _atan(
|
|
520
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, arg: Atom
|
|
521
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
522
|
+
"""C atan operation.
|
|
523
|
+
|
|
524
|
+
Examples
|
|
525
|
+
--------
|
|
526
|
+
>>> from sympy.abc import x
|
|
527
|
+
>>> from sympy.functions.elementary.trigonometric import atan
|
|
528
|
+
>>> import numpy as np
|
|
529
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _lambdify_c
|
|
530
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic, _printer
|
|
531
|
+
>>> func = _lambdify_c(_printer([(x, atan(x))], {}, {x}))
|
|
532
|
+
>>> func(np.array([np.nan]))
|
|
533
|
+
array([nan])
|
|
534
|
+
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf])) / np.pi
|
|
535
|
+
array([-0.5 , -0.25, 0. , 0.25, 0.5 ])
|
|
536
|
+
>>>
|
|
537
|
+
"""
|
|
538
|
+
if (cfunc := {
|
|
539
|
+
"float": "atanf",
|
|
540
|
+
"double": "atan",
|
|
541
|
+
"long double": "atanl",
|
|
542
|
+
}.get(c_type, None)) is None:
|
|
543
|
+
raise CompilationError(f"atan is not implemented for {c_type}")
|
|
544
|
+
code = f"{_atom2str(out, indexing, c_type)} = {cfunc}({_atom2str(arg, indexing, c_type)});"
|
|
545
|
+
return set(), set(), [code]
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
def _cos(
|
|
549
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, arg: Atom
|
|
550
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
551
|
+
"""C cos operation.
|
|
552
|
+
|
|
553
|
+
Examples
|
|
554
|
+
--------
|
|
555
|
+
>>> from sympy.abc import x
|
|
556
|
+
>>> from sympy.functions.elementary.trigonometric import cos
|
|
557
|
+
>>> import numpy as np
|
|
558
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _lambdify_c
|
|
559
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic, _printer
|
|
560
|
+
>>> func = _lambdify_c(_printer([(x, cos(x))], {}, {x}))
|
|
561
|
+
>>> func(np.array([np.nan]))
|
|
562
|
+
array([nan])
|
|
563
|
+
>>> func(np.array([0., 0.78539816, 2.35619449, 3.14159265, 3.92699082, 5.49778714, 6.28318531]))
|
|
564
|
+
array([ 1. , 0.70710678, -0.70710678, -1. , -0.70710678,
|
|
565
|
+
0.70710678, 1. ])
|
|
566
|
+
>>> func(np.array([1+1j]))
|
|
567
|
+
array([0.83373003-0.98889771j])
|
|
568
|
+
>>>
|
|
569
|
+
"""
|
|
570
|
+
cfunc = {
|
|
571
|
+
"float": "cosf",
|
|
572
|
+
"double": "cos",
|
|
573
|
+
"long double": "cosl",
|
|
574
|
+
"float complex": "ccosf",
|
|
575
|
+
"double complex": "ccos",
|
|
576
|
+
"long double complex": "ccosl",
|
|
577
|
+
}[c_type]
|
|
578
|
+
code = f"{_atom2str(out, indexing, c_type)} = {cfunc}({_atom2str(arg, indexing, c_type)});"
|
|
579
|
+
return set(), set(), [code]
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
def _exp(
|
|
583
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, arg: Atom
|
|
584
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
585
|
+
"""C exp operation.
|
|
586
|
+
|
|
587
|
+
Examples
|
|
588
|
+
--------
|
|
589
|
+
>>> from sympy.abc import x
|
|
590
|
+
>>> from sympy.functions.elementary.trigonometric import exp
|
|
591
|
+
>>> import numpy as np
|
|
592
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _lambdify_c
|
|
593
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic, _printer
|
|
594
|
+
>>> func = _lambdify_c(_printer([(x, exp(x))], {}, {x}))
|
|
595
|
+
>>> func(np.array([np.nan]))
|
|
596
|
+
array([nan])
|
|
597
|
+
>>> func(np.array([-np.inf, -1.0, 0.0, 1.0, np.inf]))
|
|
598
|
+
array([0. , 0.36787944, 1. , 2.71828183, inf])
|
|
599
|
+
>>> func(np.array([1+1j]))
|
|
600
|
+
array([1.46869394+2.28735529j])
|
|
601
|
+
>>>
|
|
602
|
+
"""
|
|
603
|
+
cfunc = {
|
|
604
|
+
"float": "expf",
|
|
605
|
+
"double": "exp",
|
|
606
|
+
"long double": "expl",
|
|
607
|
+
"float complex": "cexpf",
|
|
608
|
+
"double complex": "cexp",
|
|
609
|
+
"long double complex": "cexpl",
|
|
610
|
+
}[c_type]
|
|
611
|
+
code = f"{_atom2str(out, indexing, c_type)} = {cfunc}({_atom2str(arg, indexing, c_type)});"
|
|
612
|
+
return set(), set(), [code]
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
def _mul(
|
|
616
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, *parts: Atom
|
|
617
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
618
|
+
"""C * operation.
|
|
619
|
+
|
|
620
|
+
Examples
|
|
621
|
+
--------
|
|
622
|
+
>>> from collections import defaultdict
|
|
623
|
+
>>> from sympy.abc import x, y, z
|
|
624
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic
|
|
625
|
+
>>> _print_atomic(2 * x, x, defaultdict(lambda: ""), "float")
|
|
626
|
+
(set(), set(), ['x *= 2.0f;'])
|
|
627
|
+
>>> _print_atomic(2 * x, y, defaultdict(lambda: ""), "float")
|
|
628
|
+
(set(), set(), ['y = 2.0f * x;'])
|
|
629
|
+
>>> _print_atomic(2 * x * y, z, defaultdict(lambda: ""), "float")
|
|
630
|
+
(set(), set(), ['z = 2.0f * x;', 'z *= y;'])
|
|
631
|
+
>>>
|
|
632
|
+
"""
|
|
633
|
+
out_str = _atom2str(out, indexing, c_type)
|
|
634
|
+
if len(parts) == 0:
|
|
635
|
+
parts = (Integer(1),)
|
|
636
|
+
if len(parts) == 1:
|
|
637
|
+
return set(), set(), [f"{out_str} = {_atom2str(parts[0], indexing, c_type)};"]
|
|
638
|
+
try:
|
|
639
|
+
ind = parts.index(out)
|
|
640
|
+
except ValueError:
|
|
641
|
+
code_lines = [
|
|
642
|
+
f"{out_str} = "
|
|
643
|
+
f"{_atom2str(parts[0], indexing, c_type)} * {_atom2str(parts[1], indexing, c_type)};"
|
|
644
|
+
]
|
|
645
|
+
parts = parts[2:]
|
|
646
|
+
else:
|
|
647
|
+
code_lines = []
|
|
648
|
+
parts = tuple(p for i, p in enumerate(parts) if i != ind)
|
|
649
|
+
code_lines.extend([f"{out_str} *= {_atom2str(p, indexing, c_type)};" for p in parts])
|
|
650
|
+
return set(), set(), code_lines
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
def _pow(
|
|
654
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, base: Atom, exp: Atom
|
|
655
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
656
|
+
"""C ** operation.
|
|
657
|
+
|
|
658
|
+
Examples
|
|
659
|
+
--------
|
|
660
|
+
>>> from collections import defaultdict
|
|
661
|
+
>>> from sympy.abc import x, y, z
|
|
662
|
+
>>> import numpy as np
|
|
663
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _lambdify_c
|
|
664
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic, _printer
|
|
665
|
+
>>>
|
|
666
|
+
>>> _print_atomic(x**y, z, defaultdict(lambda: ""), "float")
|
|
667
|
+
(set(), set(), ['z = powf(x, y);'])
|
|
668
|
+
>>> _print_atomic(1/x, z, defaultdict(lambda: ""), "float")
|
|
669
|
+
(set(), set(), ['z = 1.0f / x;'])
|
|
670
|
+
>>> _print_atomic(x**2, y, defaultdict(lambda: ""), "float")
|
|
671
|
+
(set(), set(), ['y = x * x;'])
|
|
672
|
+
>>> _print_atomic(x**.5, y, defaultdict(lambda: ""), "float")
|
|
673
|
+
(set(), set(), ['y = sqrtf(x);'])
|
|
674
|
+
>>> _print_atomic(x**(1/3), y, defaultdict(lambda: ""), "float")
|
|
675
|
+
(set(), set(), ['y = cbrtf(x);'])
|
|
676
|
+
>>> _print_atomic(x**(-7/2), y, defaultdict(lambda: ""), "double")
|
|
677
|
+
(set(), {_buf}, ['y = x * x;', '_buf = x * y;', 'y *= y;', 'y *= _buf;', 'y = 1.0 / sqrt(y);'])
|
|
678
|
+
>>>
|
|
679
|
+
>>> func = _lambdify_c(_printer([(x, x**y)], {}, {x, y}))
|
|
680
|
+
>>> func(
|
|
681
|
+
... np.array([2.0 , np.nan, 0.0, 0.0, 0.0, -0.0, 2.0, 2.0]),
|
|
682
|
+
... np.array([np.nan, 2.0, 0.0, 1.0, -1.0, -1.0, np.inf, -np.inf])
|
|
683
|
+
... )
|
|
684
|
+
array([ nan, nan, 1., 0., inf, -inf, inf, 0.])
|
|
685
|
+
>>> func(np.array([1+1j]), np.array([1+1j]))
|
|
686
|
+
array([0.27395725+0.58370076j])
|
|
687
|
+
>>> func = _lambdify_c(_printer([(x, x**2)], {}, {x}))
|
|
688
|
+
>>> func(np.array([np.nan, -np.inf, -2.0, -1.0, 1.0, 2.0, np.inf]))
|
|
689
|
+
array([nan, inf, 4., 1., 1., 4., inf])
|
|
690
|
+
>>> func = _lambdify_c(_printer([(x, 1/x)], {}, {x}))
|
|
691
|
+
>>> func(np.array([np.nan, -np.inf, -2.0, -1.0, -0.0, 0.0, 1.0, 2.0, np.inf]))
|
|
692
|
+
array([ nan, -0. , -0.5, -1. , -inf, inf, 1. , 0.5, 0. ])
|
|
693
|
+
>>> func = _lambdify_c(_printer([(x, x**.5)], {}, {x}))
|
|
694
|
+
>>> func(np.array([np.nan, -np.inf, -1.0, 0.0, 1.0, 4.0, np.inf]))
|
|
695
|
+
array([nan, nan, nan, 0., 1., 2., inf])
|
|
696
|
+
>>> func = _lambdify_c(_printer([(x, x**(1/3))], {}, {x}))
|
|
697
|
+
>>> func(np.array([np.nan, -np.inf, -8.0, -1.0, 0.0, 1.0, 8.0, np.inf]))
|
|
698
|
+
array([ nan, -inf, -2., -1., 0., 1., 2., inf])
|
|
699
|
+
>>> func = _lambdify_c(_printer([(x, x**(-7/2))], {}, {x}))
|
|
700
|
+
>>> func(np.array([0.820335356007638]))
|
|
701
|
+
array([2.])
|
|
702
|
+
>>>
|
|
703
|
+
"""
|
|
704
|
+
out_str = _atom2str(out, indexing, c_type)
|
|
705
|
+
|
|
706
|
+
if exp.is_number:
|
|
707
|
+
exp_frac = Fraction(float(exp)).limit_denominator()
|
|
708
|
+
|
|
709
|
+
# a**-b <=> 1/(a**b)
|
|
710
|
+
if exp_frac.denominator <= 3 and exp_frac < 0:
|
|
711
|
+
context, alloc, lines = _pow(out, indexing, c_type, base, -exp)
|
|
712
|
+
if (match := re.search(r" = (?P<inst>.+);$", lines[-1])) is not None:
|
|
713
|
+
lines[-1] = (
|
|
714
|
+
f"{out_str} = {_atom2str(One(), indexing, c_type)} / {match['inst']};"
|
|
715
|
+
if re.fullmatch(r"\S+(?:[\(\[].*[\)\]])?", match["inst"]) else
|
|
716
|
+
f"{out_str} = {_atom2str(One(), indexing, c_type)} / ({match['inst']});"
|
|
717
|
+
)
|
|
718
|
+
else:
|
|
719
|
+
lines.append(f"{out_str} = {_atom2str(One(), indexing, c_type)}/{out_str};")
|
|
720
|
+
return context, alloc, lines
|
|
721
|
+
|
|
722
|
+
# a**(n/m) <=> (a**n)**(1/m)
|
|
723
|
+
if (
|
|
724
|
+
cfunc := { # a**(1/2) <=> sqrt(a) or a**(1/3) <=> cbrt(a)
|
|
725
|
+
(2, "float"): "sqrtf",
|
|
726
|
+
(2, "double"): "sqrt",
|
|
727
|
+
(2, "long double"): "sqrtl",
|
|
728
|
+
(2, "float complex"): "csqrtf",
|
|
729
|
+
(2, "double complex"): "csqrt",
|
|
730
|
+
(2, "long double complex"): "csqrtl",
|
|
731
|
+
(3, "float"): "cbrtf",
|
|
732
|
+
(3, "double"): "cbrt",
|
|
733
|
+
(3, "long double"): "cbrtl",
|
|
734
|
+
}.get((exp_frac.denominator, c_type), None)
|
|
735
|
+
) is not None:
|
|
736
|
+
context, alloc, lines = _pow(out, indexing, c_type, base, Rational(exp_frac.numerator))
|
|
737
|
+
if (match := re.search(r" = (?P<inst>.+);$", lines[-1])) is not None:
|
|
738
|
+
lines[-1] = f"{out_str} = {cfunc}({match['inst']});"
|
|
739
|
+
else:
|
|
740
|
+
lines.append(f"{out_str} = {cfunc}({out_str});")
|
|
741
|
+
return context, alloc, lines
|
|
742
|
+
|
|
743
|
+
# a**n, n integer, use a**n <=> (a**2)**(n/2)
|
|
744
|
+
if exp_frac.denominator == 1:
|
|
745
|
+
return _pow_pos_integer(out, indexing, c_type, base, Integer(exp_frac.numerator))
|
|
746
|
+
|
|
747
|
+
# general case
|
|
748
|
+
cfunc = {
|
|
749
|
+
"float": "powf",
|
|
750
|
+
"double": "pow",
|
|
751
|
+
"long double": "powl",
|
|
752
|
+
"float complex": "cpowf",
|
|
753
|
+
"double complex": "cpow",
|
|
754
|
+
"long double complex": "cpowl",
|
|
755
|
+
}[c_type]
|
|
756
|
+
return (
|
|
757
|
+
set(),
|
|
758
|
+
set(),
|
|
759
|
+
[
|
|
760
|
+
f"{out_str} = {cfunc}("
|
|
761
|
+
f"{_atom2str(base, indexing, c_type)}, {_atom2str(exp, indexing, c_type)});"
|
|
762
|
+
],
|
|
763
|
+
)
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
def _pow_pos_integer(
|
|
767
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, base: Atom, exp: Atom
|
|
768
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
769
|
+
"""Manage the special case of power where the exponant is a positive integer.
|
|
770
|
+
|
|
771
|
+
Examples
|
|
772
|
+
--------
|
|
773
|
+
>>> from collections import defaultdict
|
|
774
|
+
>>> from sympy.abc import x, y
|
|
775
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _pow_pos_integer
|
|
776
|
+
>>>
|
|
777
|
+
>>> # case allocation in new var
|
|
778
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 0)
|
|
779
|
+
(set(), set(), ['y = 1.0f;'])
|
|
780
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 1)
|
|
781
|
+
(set(), set(), ['y = x;'])
|
|
782
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 2)
|
|
783
|
+
(set(), set(), ['y = x * x;'])
|
|
784
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 3)
|
|
785
|
+
(set(), set(), ['y = x * x;', 'y *= x;'])
|
|
786
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 4)
|
|
787
|
+
(set(), set(), ['y = x * x;', 'y *= y;'])
|
|
788
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 5)
|
|
789
|
+
(set(), set(), ['y = x * x;', 'y *= y;', 'y *= x;'])
|
|
790
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 6)
|
|
791
|
+
(set(), {_buf}, ['y = x * x;', '_buf = y;', 'y *= y;', 'y *= _buf;'])
|
|
792
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 7)
|
|
793
|
+
(set(), {_buf}, ['y = x * x;', '_buf = x * y;', 'y *= y;', 'y *= _buf;'])
|
|
794
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 8)
|
|
795
|
+
(set(), set(), ['y = x * x;', 'y *= y;', 'y *= y;'])
|
|
796
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 9)
|
|
797
|
+
(set(), set(), ['y = x * x;', 'y *= y;', 'y *= y;', 'y *= x;'])
|
|
798
|
+
>>> _pow_pos_integer(y, defaultdict(lambda: ""), "float", x, 10)
|
|
799
|
+
(set(), {_buf}, ['y = x * x;', '_buf = y;', 'y *= y;', 'y *= y;', 'y *= _buf;'])
|
|
800
|
+
>>>
|
|
801
|
+
>>> # case allocation inplace
|
|
802
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 0)
|
|
803
|
+
(set(), set(), ['x = 1.0f;'])
|
|
804
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 1)
|
|
805
|
+
(set(), set(), ['x = x;'])
|
|
806
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 2)
|
|
807
|
+
(set(), set(), ['x *= x;'])
|
|
808
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 3)
|
|
809
|
+
(set(), {_buf}, ['_buf = x;', 'x *= x;', 'x *= _buf;'])
|
|
810
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 4)
|
|
811
|
+
(set(), set(), ['x *= x;', 'x *= x;'])
|
|
812
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 5)
|
|
813
|
+
(set(), {_buf}, ['_buf = x;', 'x *= x;', 'x *= x;', 'x *= _buf;'])
|
|
814
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 6)
|
|
815
|
+
(set(), {_buf}, ['x *= x;', '_buf = x;', 'x *= x;', 'x *= _buf;'])
|
|
816
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 7)
|
|
817
|
+
(set(), {_buf}, ['_buf = x;', 'x *= x;', '_buf *= x;', 'x *= x;', 'x *= _buf;'])
|
|
818
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 8)
|
|
819
|
+
(set(), set(), ['x *= x;', 'x *= x;', 'x *= x;'])
|
|
820
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 9)
|
|
821
|
+
(set(), {_buf}, ['_buf = x;', 'x *= x;', 'x *= x;', 'x *= x;', 'x *= _buf;'])
|
|
822
|
+
>>> _pow_pos_integer(x, defaultdict(lambda: ""), "float", x, 10)
|
|
823
|
+
(set(), {_buf}, ['x *= x;', '_buf = x;', 'x *= x;', 'x *= x;', 'x *= _buf;'])
|
|
824
|
+
>>>
|
|
825
|
+
"""
|
|
826
|
+
if exp in {0, 1}:
|
|
827
|
+
return _mul(out, indexing, c_type, *([base]*exp))
|
|
828
|
+
context, alloc, lines = set(), set(), []
|
|
829
|
+
|
|
830
|
+
# formal general code
|
|
831
|
+
symb_code = []
|
|
832
|
+
buf = Symbol("_buf")
|
|
833
|
+
state = {out: base, buf: Integer(1)}
|
|
834
|
+
while exp > 1:
|
|
835
|
+
if exp % 2: # odd exponant
|
|
836
|
+
symb_code.append(Equality(
|
|
837
|
+
buf,
|
|
838
|
+
(
|
|
839
|
+
(state[buf] if state[buf].is_Atom else buf)
|
|
840
|
+
* (state[out] if state[out].is_Atom else out)
|
|
841
|
+
)
|
|
842
|
+
))
|
|
843
|
+
state[buf] = state[buf] * state[out]
|
|
844
|
+
state[out] = state[out].subs(state, simultaneous=True)
|
|
845
|
+
symb_code.append(Equality(out, (state[out] if state[out].is_Atom else out)**2))
|
|
846
|
+
state[out] = state[out] * state[out]
|
|
847
|
+
state[buf] = state[buf].subs(state, simultaneous=True)
|
|
848
|
+
exp //= 2
|
|
849
|
+
symb_code.append(
|
|
850
|
+
Equality(
|
|
851
|
+
out,
|
|
852
|
+
(
|
|
853
|
+
(state[out] if state[out].is_Atom else out)
|
|
854
|
+
* (state[buf] if state[buf].is_Atom else buf)
|
|
855
|
+
)
|
|
856
|
+
)
|
|
857
|
+
)
|
|
858
|
+
|
|
859
|
+
# simplify
|
|
860
|
+
symb_code = [eq for eq in symb_code if isinstance(eq, Equality)] # remove `x = x` lines
|
|
861
|
+
if any(buf in eq.args[1].free_symbols for eq in symb_code):
|
|
862
|
+
alloc.add(buf)
|
|
863
|
+
else: # if buf is not used
|
|
864
|
+
symb_code = [eq for eq in symb_code if eq.args[0] != buf] # remove `buf = ...` lines
|
|
865
|
+
# remove first item of var declaration
|
|
866
|
+
buf = { # at each line index, associate the new declared var
|
|
867
|
+
i: eq.args[0] for i, eq in enumerate(symb_code) if eq.args[0] not in eq.args[1].free_symbols
|
|
868
|
+
}
|
|
869
|
+
buf = set.union( # the index to remove
|
|
870
|
+
set(), *(set([i for i, v_ in buf.items() if v_ is v][:-1]) for v in set(buf.values()))
|
|
871
|
+
)
|
|
872
|
+
symb_code = [eq for i, eq in enumerate(symb_code) if i not in buf]
|
|
873
|
+
|
|
874
|
+
# # verification
|
|
875
|
+
# state = {base: base}
|
|
876
|
+
# for equ in symb_code:
|
|
877
|
+
# state[equ.args[0]] = equ.args[1].xreplace(state)
|
|
878
|
+
# print(state[out])
|
|
879
|
+
|
|
880
|
+
# convert into C code
|
|
881
|
+
for var, buf in (eq.args for eq in symb_code):
|
|
882
|
+
if buf.is_Atom:
|
|
883
|
+
new_context, new_alloc, new_lines = _mul(var, indexing, c_type, buf)
|
|
884
|
+
elif isinstance(buf, Pow):
|
|
885
|
+
new_context, new_alloc, new_lines = _mul(var, indexing, c_type, *([buf.base]*buf.exp))
|
|
886
|
+
else: # case Mul
|
|
887
|
+
new_context, new_alloc, new_lines = _mul(var, indexing, c_type, *buf.args)
|
|
888
|
+
context |= new_context
|
|
889
|
+
alloc |= new_alloc
|
|
890
|
+
lines.extend(new_lines)
|
|
891
|
+
|
|
892
|
+
return context, alloc, lines
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
def _sin(
|
|
896
|
+
out: Symbol, indexing: collections.defaultdict[Symbol, str], c_type: str, arg: Atom
|
|
897
|
+
) -> tuple[set[str], set[Symbol], list[str]]:
|
|
898
|
+
"""C sin operation.
|
|
899
|
+
|
|
900
|
+
Examples
|
|
901
|
+
--------
|
|
902
|
+
>>> from sympy.abc import x
|
|
903
|
+
>>> from sympy.functions.elementary.trigonometric import sin
|
|
904
|
+
>>> import numpy as np
|
|
905
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _lambdify_c
|
|
906
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _print_atomic, _printer
|
|
907
|
+
>>> func = _lambdify_c(_printer([(x, sin(x))], {}, {x}))
|
|
908
|
+
>>> func(np.array([np.nan]))
|
|
909
|
+
array([nan])
|
|
910
|
+
>>> func(np.array([0.78539816, 1.57079633, 2.35619449, 3.92699082, 4.71238898, 5.49778714]))
|
|
911
|
+
array([ 0.70710678, 1. , 0.70710678, -0.70710678, -1. ,
|
|
912
|
+
-0.70710678])
|
|
913
|
+
>>> func(np.array([1+1j]))
|
|
914
|
+
array([1.29845758+0.63496391j])
|
|
915
|
+
>>>
|
|
916
|
+
"""
|
|
917
|
+
cfunc = {
|
|
918
|
+
"float": "sinf",
|
|
919
|
+
"double": "sin",
|
|
920
|
+
"long double": "sinl",
|
|
921
|
+
"float complex": "csinf",
|
|
922
|
+
"double complex": "csin",
|
|
923
|
+
"long double complex": "csinl",
|
|
924
|
+
}[c_type]
|
|
925
|
+
code = f"{_atom2str(out, indexing, c_type)} = {cfunc}({_atom2str(arg, indexing, c_type)});"
|
|
926
|
+
return set(), set(), [code]
|