Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:23:16+00:00 2026-06-15T12:23:16+00:00

import numpy as np def readMatrix(filename): rows = [] for line in open(filename): columns

  • 0
import numpy as np
def readMatrix(filename):
    rows = []
    for line in open(filename):
        columns = []
        for number in string.split(line):
            columns.append(float(number))
        rows.append(columns)
    return numpy.array(rows)

def writeMatrix(a, filename):
    f = open(filename, 'w')
    for row in a:
        for number in row:
            f.write(str(number) + ' ')
        f.write('\n')
    f.close()

def TaylorMatrixExp(A):
    I = identity(len(A))
    return (I + A + (1./2.)*(dot(A,A)) + (1./6.)*(dot(dot(A,A),A)) + (1./24.)*(dot(dot(A,A),dot(A,A))))

A = readMatrix('matrix.txt')

l, v = eig(A)

L = identity(len(l))

for i in xrange(len(l)):
    L[i][i] = array(exp(l))[i]

VLV = dot(dot(v,L),inv(v))

writeMatrix(VLV,'expA.txt')

ExponentA = TaylorMatrixExp(A)
writeMatrix(ExponentA,'expA.txt')  

The matrix it reads is:
2 2
16 6

I’ve defined two 3 functions, readMatrix(reads a matrix from a textfile), writeMatrix(writes matrix to a file), and TaylorMatrixExp(takes array and expands it). I initially use readMatrix to read a text file containing the above matrix and place it in array A. I take the eigenvalues of A and place it in array l as well as eigenvectors of A and place it in array v. I eventually place the values of array l across the diagonal of an identity matrix. Then I call the writeMatrix function and write exponent to ‘expA.txt’ and then call writeMatrix function again and write matrix ExponentA to ‘expA.txt’. However, it replaces the original matrix and I don’t want it to do that.

and I want it to write to a file
some# some#
some# some#

some#2 some#2
some#2 some#2

but instead it replaces the first matrix
some#2 some#2
some#2 some#2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:23:17+00:00Added an answer on June 15, 2026 at 12:23 pm
    f = open(filename, 'a')
    

    Lets you append to the file rather than rewrite it, which is what you’re currently doing with the 'w' argument, which is why that matrix is replaced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following: import numpy as np cimport numpy as np DTYPE = np.float
I have the following code: import numpy def numpysum(n): a = numpy.arange(n) ** 2
import numpy as np def L2Norm(f, x, y, d=0.00001): dudx = (f(x+d,y) - f(x-d,y))
import numpy a = numpy.array([20090913, 20101020, 20110125]) Can you explain why numpy.datetime64(a.astype(S8).tolist()) converts correctly
I find myself typing import numpy as np almost every single time I fire
What is the difference between import numpy as np np.dot(a,b) and import numpy as
In the following example In [8]: import numpy as np In [9]: strings =
I have installed Numpy using ActivePython and when I try to import numpy module,
The textbook examples of multiple unpacking assignment are something like: import numpy as NP
In short: I have two matrices (or arrays): import numpy block_1 = numpy.matrix([[ 0,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.