2

Is it possible using numpy for python (versions 3.3) to write the code for building an nxn matrix, without specifying n? I need to index the entries as A_i,j or something like that, but I dont even know how to define the A_i,j so that they are actually objects. I thought something like this might work:

n    
i=1
j=1

when i (is less than) n+1

  when j (is less than) i+1
   A_i,j= f(i,j)
   j+=1

i+=1

but this does not work...any suggestions? My ultimate Goal is to write the QR decomposition for an arbitrary nxn matrix. But I need to know how to define the matrix that I am working on first. I am very new to python and thus numpy and so dont know much of anything. any help would be greatly appreciated. I am also new to stackexchange so sorry for the bad piece of code i have there. (is less than) is supposed to mean the triangle sign missing the base with head pointing to the left- that is the obvious less than symbol

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
user3495725
  • 21
  • 1
  • 2

1 Answers1

4

You can create an empty nxn array:

import itertools
import numpy as np

my_array = np.empty([n, n])

Then set the value at coordinate i, j to f(i, j).

for i, j in itertools.product(range(n), range(n)):
    my_array[i, j] = f(i, j)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • thanks a lot for your reply this is great. I still can't get it to work however. i get an error at the line containing my_array saying that 'module' object has no attribute 'fromfunction'. does this mean that my numpy program does not know where to find the fromfunction? do i need to download or install some file containing it? thanks again. – user3495725 Apr 04 '14 at 01:57
  • @user3495725 I have edited my answer, no longer using `fromfunction` – jonrsharpe Apr 04 '14 at 07:40