前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在线矩阵微积分工具,可以生成 Python/Latex 代码哦!

在线矩阵微积分工具,可以生成 Python/Latex 代码哦!

作者头像
量化投资与机器学习微信公众号
发布2018-01-29 19:10:49
1.3K0
发布2018-01-29 19:10:49
举报

社区在线技术交流群

https://bbs.mlqi.org

(大家多去逛逛哈)

今天给大家介绍一个实用的在线小工具,矩阵微积分在线生成 Python/Latex 代码。

链接:

http://www.matrixcalculus.org/matrixCalculus

Python代码如下:

代码语言:js
复制
"""
d/dx x'*A*x + c*sin(y)'*x = 2*A*x+c*sin(y)
A is a matrix
c is a scalar
y is a vector
x is a vector
"""

from __future__ import division, print_function, absolute_import
   
import numpy as np
   
def fAndG(A, c, x, y):
   assert(type(A) == np.ndarray)
   dim = A.shape
   assert(len(dim) == 2)
   A_rows = dim[0]
   A_cols = dim[1]
   if type(c) == np.ndarray:
       dim = c.shape
       assert(dim == (1, ))
   assert(type(x) == np.ndarray)
   dim = x.shape
   assert(len(dim) == 1)
   x_rows = dim[0]
   assert(type(y) == np.ndarray)
   dim = y.shape
   assert(len(dim) == 1)
   y_rows = dim[0]
   assert(A_cols == x_rows == y_rows == A_rows)

   t_0 = np.dot(A, x)
   t_1 = np.sin(y)
   functionValue = (np.dot(x, t_0) + (c * np.dot(t_1, x)))
   gradient = ((2 * t_0) + (c * t_1))

   return functionValue, gradient

def generateRandomData():
   A = np.random.randn(3, 3)
   c = np.random.randn(1)
   x = np.random.randn(3)
   y = np.random.randn(3)
   return A, c, x, y

if __name__ == '__main__':
   A, c, x, y = generateRandomData()
   functionValue, gradient = fAndG(A, c, x, y)
   print('functionValue = ', functionValue)
   print('gradient = ', gradient)

Latex代码如下:

代码语言:javascript
复制
\documentclass[12pt]{article}
\usepackage{amsmath,amsthm,amssymb}
\begin{document}
function:
\begin{align*}
 f = x^\top \cdot A\cdot x+c\cdot \sin(y)^\top \cdot x
\end{align*}

gradient:
\begin{align*}
 \frac{\partial f}{\partial x} = 2\cdot A\cdot x+c\cdot \sin(y)
\end{align*}
\end{document}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 量化投资与机器学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档