Python和C++(CUDA)及Arduino雅可比矩阵导图

2024-08-30 16:12

本文主要是介绍Python和C++(CUDA)及Arduino雅可比矩阵导图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

🎯要点

  1. 对比三种方式计算
  2. 读取二维和三维三角形四边形和六面体网格
  3. 运动学奇异点处理
  4. 医学图像成像组学分析
  5. 特征敏感度增强
  6. 机械臂路径规划和手臂空间操作变换
  7. 苹果手机物理稳定性中间轴定理
    在这里插入图片描述

Python雅可比矩阵

多变量向量值函数的雅可比矩阵推广了多变量标量值函数的梯度,而这又推广了单变量标量值函数的导数。换句话说,多变量标量值函数的雅可比矩阵是其梯度(的转置),而单变量标量值函数的梯度是其导数。

在函数可微的每个点,其雅可比矩阵也可以被认为是描述函数在该点附近局部施加的“拉伸”、“旋转”或“变换”量。例如,如果使用 ( x ′ , y ′ ) = f ( x , y ) \left(x^{\prime}, y^{\prime}\right)= f (x, y) (x,y)=f(x,y) 平滑变换图像,则雅可比矩阵 J f ( x , y ) J _{ f }( x, y) Jf(x,y),描述了 ( x , y ) (x, y) (x,y)邻域中的图像如何变换。如果函数在某点可微,其微分在坐标系中由雅可比矩阵给出。然而,函数不需要可微才能定义其雅可比矩阵,因为只需要存在其一阶偏导数。

考虑以下向量函数,该函数将 n n n 维向量 x ∈ R n x \in R ^n xRn 作为输入,并将该向量映射到 m m m 维向量:
f ( x ) = [ f 1 ( x 1 , x 2 , x 3 , … , x n ) f 2 ( x 1 , x 2 , x 3 , … , x n ) ⋮ f m ( x 1 , x 2 , x 3 , … , x n ) ] f ( x )=\left[\begin{array}{c} f_1\left(x_1, x_2, x_3, \ldots, x_n\right) \\ f_2\left(x_1, x_2, x_3, \ldots, x_n\right) \\ \vdots \\ f_m\left(x_1, x_2, x_3, \ldots, x_n\right) \end{array}\right] f(x)= f1(x1,x2,x3,,xn)f2(x1,x2,x3,,xn)fm(x1,x2,x3,,xn)

其中向量 x x x 定义为
x = [ x 1 x 2 ⋮ x n ] x =\left[\begin{array}{c} x_1 \\ x_2 \\ \vdots \\ x_n \end{array}\right] x= x1x2xn
非线性向量函数 f f f 产生 m m m 维向量
[ f 1 ( x 1 , x 2 , x 3 , … , x n ) f 2 ( x 1 , x 2 , x 3 , … , x n ) ⋮ f m ( x 1 , x 2 , x 3 , … , x n ) ] \left[\begin{array}{c} f_1\left(x_1, x_2, x_3, \ldots, x_n\right) \\ f_2\left(x_1, x_2, x_3, \ldots, x_n\right) \\ \vdots \\ f_m\left(x_1, x_2, x_3, \ldots, x_n\right) \end{array}\right] f1(x1,x2,x3,,xn)f2(x1,x2,x3,,xn)fm(x1,x2,x3,,xn)
其条目是 m m m 函数 f i , i = 1 , 2 , … , n f_i, i=1,2, \ldots, n fi,i=1,2,,n,将向量 x x x 的条目映射为标量数。

函数 f ( ⋅ ) f (\cdot) f() 的雅可比矩阵是 m m m × n n n 维偏导数矩阵,定义为
∂ f ∂ x = [ ∂ f 1 ∂ x 1 ∂ f 1 ∂ x 2 ⋯ ∂ f 1 ∂ x n ∂ f 2 ∂ x 1 ∂ f 2 ∂ x 2 ⋯ ∂ f 2 ∂ x n ⋮ ⋮ ⋮ ∂ f m ∂ x 1 ∂ f m ∂ x 2 … ∂ f m ∂ x n ] \frac{\partial f }{\partial x }=\left[\begin{array}{cccc} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \cdots & \frac{\partial f_1}{\partial x_n} \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & \cdots & \frac{\partial f_2}{\partial x_n} \\ \vdots & \vdots & & \vdots \\ \frac{\partial f_m}{\partial x_1} & \frac{\partial f_m}{\partial x_2} & \ldots & \frac{\partial f_m}{\partial x_n} \end{array}\right] xf= x1f1x1f2x1fmx2f1x2f2x2fmxnf1xnf2xnfm
该矩阵的第一行由 f 1 ( ⋅ ) f_1(\cdot) f1() 分别相对于 x 1 、 x 2 、 … 、 x n x_1、x_2、\ldots、x_n x1x2xn 的偏导数组成。类似地,该矩阵的第二行由 f 2 ( ⋅ ) f_2(\cdot) f2() 分别相对于 x 1 、 x 2 、 … 、 x n x_1、x_2、\ldots、x_n x1x2xn 的偏导数组成。以同样的方式,我们构造雅可比矩阵的其他行。

在这里,我们展示了用于符号计算雅可比矩阵和创建 Python 函数的 Python 脚本,该函数将返回给定输入向量 x x x 的雅可比矩阵的数值。为了验证 Python 实现,让我们考虑以下测试用例函数
f = [ x 1 x 2 sin ⁡ ( x 1 ) cos ⁡ ( x 3 ) x 3 e x 4 ] f =\left[\begin{array}{c} x_1 x_2 \\ \sin \left(x_1\right) \\ \cos \left(x_3\right) \\ x_3 e^{x_4} \end{array}\right] f= x1x2sin(x1)cos(x3)x3ex4
其中 x x x
x = [ x 1 x 2 x 3 x 4 ] x =\left[\begin{array}{l} x_1 \\ x_2 \\ x_3 \\ x_4 \end{array}\right] x= x1x2x3x4

f 1 ( x 1 , x 2 , x 3 , x 4 ) = x 1 x 2 f 2 ( x 1 , x 2 , x 3 , x 4 ) = sin ⁡ ( x 1 ) f 3 ( x 1 , x 2 , x 3 , x 4 ) = cos ⁡ ( x 3 ) f 4 ( x 1 , x 2 , x 3 , x 4 ) = x 3 e x 4 \begin{aligned} & f_1\left(x_1, x_2, x_3, x_4\right)=x_1 x_2 \\ & f_2\left(x_1, x_2, x_3, x_4\right)=\sin \left(x_1\right) \\ & f_3\left(x_1, x_2, x_3, x_4\right)=\cos \left(x_3\right) \\ & f_4\left(x_1, x_2, x_3, x_4\right)=x_3 e^{x_4} \end{aligned} f1(x1,x2,x3,x4)=x1x2f2(x1,x2,x3,x4)=sin(x1)f3(x1,x2,x3,x4)=cos(x3)f4(x1,x2,x3,x4)=x3ex4
该函数的雅可比行列式是
∂ f ∂ x = [ ∂ f 1 ∂ x 1 ∂ f 1 ∂ x 2 ∂ f 1 ∂ x 3 ∂ f 1 ∂ x 4 ∂ f 2 ∂ x 1 ∂ f 2 ∂ x 2 ∂ f 2 ∂ x 3 ∂ f 2 ∂ x 4 ∂ f 3 ∂ x 1 ∂ f 3 ∂ x 2 ∂ f 3 ∂ x 3 ∂ f 3 ∂ x 4 ∂ f 4 ∂ x 1 ∂ f 4 ∂ x 2 ∂ f 4 ∂ x 3 ∂ f 4 ∂ x 4 ] \frac{\partial f }{\partial x }=\left[\begin{array}{llll} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \frac{\partial f_1}{\partial x_3} & \frac{\partial f_1}{\partial x_4} \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & \frac{\partial f_2}{\partial x_3} & \frac{\partial f_2}{\partial x_4} \\ \frac{\partial f_3}{\partial x_1} & \frac{\partial f_3}{\partial x_2} & \frac{\partial f_3}{\partial x_3} & \frac{\partial f_3}{\partial x_4} \\ \frac{\partial f_4}{\partial x_1} & \frac{\partial f_4}{\partial x_2} & \frac{\partial f_4}{\partial x_3} & \frac{\partial f_4}{\partial x_4} \end{array}\right] xf= x1f1x1f2x1f3x1f4x2f1x2f2x2f3x2f4x3f1x3f2x3f3x3f4x4f1x4f2x4f3x4f4
通过计算这些偏导数,我们得到
∂ f ∂ x = [ x 2 x 1 0 0 cos ⁡ ( x 1 ) 0 0 0 0 0 − sin ⁡ ( x 3 ) 0 0 0 e x 4 x 3 e x 4 ] \frac{\partial f }{\partial x }=\left[\begin{array}{cccc} x_2 & x_1 & 0 & 0 \\ \cos \left(x_1\right) & 0 & 0 & 0 \\ 0 & 0 & -\sin \left(x_3\right) & 0 \\ 0 & 0 & e^{x_4} & x_3 e^{x_4} \end{array}\right] xf= x2cos(x1)00x100000sin(x3)ex4000x3ex4

import numpy as np
from sympy import *init_printing()x=MatrixSymbol('x',4,1)
f=Matrix([[x[0]*x[1]],[sin(x[0])],[cos(x[2])],[x[2]*E**(x[3])]])JacobianSymbolic=f.jacobian(x)
JacobianFunction=lambdify(x,JacobianSymbolic)
testCaseVector=np.array([[1],[1],[1],[1]])
JacobianNumerical=JacobianFunction(testCaseVector)

定义符号向量“x”如下

x=MatrixSymbol('x',4,1)

非线性向量函数“f”定义为

=Matrix([[x[0]*x[1]],[sin(x[0])],[cos(x[2])],[x[2]*E**(x[3])]])
JacobianSymbolic=f.jacobian(x)
JacobianFunction=lambdify(x,JacobianSymbolic)

测试向量处评估雅可比行列式。

testCaseVector=np.array([[1],[1],[1],[1]])
JacobianNumerical=JacobianFunction(testCaseVector)

存储在“JacobianNumerical”中的结果是一个 NumPy 数值数组(矩阵),可用于进一步计算。

示例:TensorFlow雅可比矩阵

%tensorflow_version 1.x
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import numpy as np
import statsmodels.api as sm
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from tqdm import tqdm
import tensorflow as tfnp.random.seed (245)
nobs =10000x1= np.random.normal(size=nobs ,scale=1)
x2= np.random.normal(size=nobs ,scale=1)
x3= np.random.normal(size=nobs ,scale=1)
x4= np.random.normal(size=nobs ,scale=1)
x5= np.random.normal(size=nobs ,scale=1)X= np.c_[np.ones((nobs ,1)),x1,x2,x3,x4,x5]y= np.cos(x1) + np.sin(x2) + 2*x3 + x4 + 0.01*x5 + np.random.normal(size=nobs , scale=0.01)LR=0.05Neuron_Out=1
Neuron_Hidden1=64
Neuron_Hidden2=32Activate_output='linear'
Activate_hidden='relu' Optimizer= SGD(lr=LR)
loss='mean_squared_error'from sklearn.model_selection import train_test_split
x_train , x_test , y_train , y_test = train_test_split(X, y, test_size =0.15, random_state =77)from tensorflow import set_random_seed
set_random_seed (245)sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())model_ANN= Sequential()model_ANN.add(Dense(Neuron_Hidden1, activation=Activate_hidden, input_shape=(6,), use_bias=True))
model_ANN.add(Dense(Neuron_Hidden2, activation=Activate_hidden, use_bias=True))model_ANN.add(Dense(Neuron_Out, activation=Activate_output,use_bias=True))
model_ANN.summary()model_ANN.compile(loss=loss, optimizer=Optimizer, metrics=['accuracy'])history_ANN=model_ANN.fit(
x_train, 
y_train, 
epochs=125)def jacobian_tensorflow(x):jacobian_matrix = []for m in range(Neuron_Out):grad_func = tf.gradients(model_ANN.output[:, m],model_ANN.input)gradients = sess.run(grad_func, feed_dict={model_ANN.input: x})  jacobian_matrix.append(gradients[0][0,:])return np.array(jacobian_matrix)jacobian_tensorflow(x_train)

👉更新:亚图跨际

这篇关于Python和C++(CUDA)及Arduino雅可比矩阵导图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1121302

相关文章

使用Python创建一个能够筛选文件的PDF合并工具

《使用Python创建一个能够筛选文件的PDF合并工具》这篇文章主要为大家详细介绍了如何使用Python创建一个能够筛选文件的PDF合并工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录背景主要功能全部代码代码解析1. 初始化 wx.Frame 窗口2. 创建工具栏3. 创建布局和界面控件4

一文详解如何在Python中使用Requests库

《一文详解如何在Python中使用Requests库》:本文主要介绍如何在Python中使用Requests库的相关资料,Requests库是Python中常用的第三方库,用于简化HTTP请求的发... 目录前言1. 安装Requests库2. 发起GET请求3. 发送带有查询参数的GET请求4. 发起PO

Python与DeepSeek的深度融合实战

《Python与DeepSeek的深度融合实战》Python作为最受欢迎的编程语言之一,以其简洁易读的语法、丰富的库和广泛的应用场景,成为了无数开发者的首选,而DeepSeek,作为人工智能领域的新星... 目录一、python与DeepSeek的结合优势二、模型训练1. 数据准备2. 模型架构与参数设置3

Python进行PDF文件拆分的示例详解

《Python进行PDF文件拆分的示例详解》在日常生活中,我们常常会遇到大型的PDF文件,难以发送,将PDF拆分成多个小文件是一个实用的解决方案,下面我们就来看看如何使用Python实现PDF文件拆分... 目录使用工具将PDF按页数拆分将PDF的每一页拆分为单独的文件将PDF按指定页数拆分根据页码范围拆分

Python中常用的四种取整方式分享

《Python中常用的四种取整方式分享》在数据处理和数值计算中,取整操作是非常常见的需求,Python提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助... 目录引言向零取整(Truncate)向下取整(Floor)向上取整(Ceil)四舍五入(Round)四种取整方式的对比综合示例应

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi

Python中多线程和多进程的基本用法详解

《Python中多线程和多进程的基本用法详解》这篇文章介绍了Python中多线程和多进程的相关知识,包括并发编程的优势,多线程和多进程的概念、适用场景、示例代码,线程池和进程池的使用,以及如何选择合适... 目录引言一、并发编程的主要优势二、python的多线程(Threading)1. 什么是多线程?2.