Last active
November 15, 2023 20:07
-
-
Save yudium/a13f197407b63075da7c5284bfaf1b72 to your computer and use it in GitHub Desktop.
From Python Array to Ms. Word Matrix Equation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you should install numpy package with `pip install numpy` command | |
import numpy as np | |
def OUTPUT(arr, transpose=False, decimals=8): | |
# 2D array is not need [1] so we should check the dimension | |
if np.asarray(arr).ndim == 1: | |
# 1D array can't get transpose if we not add '[]' at before and after array's variable. Reference: [1] | |
arr = np.asarray([arr]) | |
else: | |
# 2D array is fine, not like [1] | |
arr = np.asarray(arr) | |
if transpose: | |
# thanks to [1] so we can also transpose 1D, not just 2D | |
arr = arr.T | |
# round the number to @param decimals | |
arr = np.around(arr, decimals=decimals) | |
row_num = arr.shape[0] | |
col_num = arr.shape[1] | |
if (col_num == 1): | |
result = '[■(' | |
result += '@'.join([str(arr[row][0]) for row in range(row_num)]) | |
result += ')]' | |
elif (row_num == 1): | |
result = '[■(' | |
result += '&'.join([str(arr[0][col]) for col in range(col_num)]) | |
result += ')]' | |
else: | |
result = '[■(' | |
result += '@'.join(['&'.join([str(arr[row, col]) for col in range(col_num)]) for row in range(row_num)]) | |
result += ')]' | |
print(result) | |
# EXAMPLE USAGE | |
OUTPUT( | |
[0.0015875153895135195, 0.7887987865331551718], | |
transpose=False, | |
decimals=8 # round to 8 decimals | |
) | |
# Output: [■(0.00158752&0.78879879)] |
do you have a guide on how to create the matrix on Ms. Word from this matrix equation?
do you have a guide on how to create the matrix on Ms. Word from this matrix equation?
- Create Equation in Word document (Alt+=)
- Insert OUTPUT result from console into created equation
- In the context menu of the created equation, select "Professional"
It worked for me (Microsoft Office 2013)
Thank you so much !!!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is useful if you have to paste many of your python array to Ms. Word document.