Skip to content

Instantly share code, notes, and snippets.

@yudium
Last active November 15, 2023 20:07
Show Gist options
  • Save yudium/a13f197407b63075da7c5284bfaf1b72 to your computer and use it in GitHub Desktop.
Save yudium/a13f197407b63075da7c5284bfaf1b72 to your computer and use it in GitHub Desktop.
From Python Array to Ms. Word Matrix Equation
# 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)]
@yudium
Copy link
Author

yudium commented Oct 5, 2019

This is useful if you have to paste many of your python array to Ms. Word document.

@c3llus
Copy link

c3llus commented Apr 14, 2021

do you have a guide on how to create the matrix on Ms. Word from this matrix equation?

@Damiryh
Copy link

Damiryh commented Nov 21, 2022

do you have a guide on how to create the matrix on Ms. Word from this matrix equation?

  1. Create Equation in Word document (Alt+=)
  2. Insert OUTPUT result from console into created equation
  3. In the context menu of the created equation, select "Professional"

It worked for me (Microsoft Office 2013)

@farid141
Copy link

Thank you so much !!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment