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 9132271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:17:01+00:00 2026-06-17T08:17:01+00:00

best wishes for 2013! I’m using SciPy’s weave inline with some C++ code of

  • 0

best wishes for 2013!

I’m using SciPy’s weave inline with some C++ code of mine to transpose huge matrices (about 200.000 x 15). It works like a charm, but I have a question about typecasting:

My input matrix is read from a file, comma separated etc., so all the entries are strings rather than floats (‘0.551’ rather than 0.551). This doesn’t affect the way the transpose function works, but later on I have to convert certain rows into numpy float arrays anyways, so I was wondering if this could be done in the C++ code instead. Let me explain with some code:

def transpose(lines, N, x):
    code =  """
            py::list matrix;
            for(int i = 0; i < x; i++) {
                py::list line;
                if(i == 1) { continue; }
                for(int j = 0; j < N; j++) {
                    line.append(lines[j][i]);
                }
                matrix.append(line);
            }
            return_val = matrix;
            """
    return weave.inline(code, ['lines', 'N', 'x'])

matrix = [['0.5','0.1'],['0.2','0.2']]
matrixT = transpose(matrix, len(matrix), len(matrix[0]))
final_result = np.array(matrixT[0], dtype=float)

In the example my small matrix will be transposed and my example result will be the first row of the transposed matrix converted to a numpy array of dtype float. Can this be done in C++ code instead? I’ve tried using double x = (double) lines[j][i] and things like that, but it somehow doesn’t work for appending to a py::list object.

  • 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-17T08:17:02+00:00Added an answer on June 17, 2026 at 8:17 am

    The following can do the whole thing you are after:

    def transpose(lines):
        code =  """
                for(int i = 0; i < x; i++) {
                    for(int j = 0; j < N; j++) {
                        out[j + i * N] = atof(lines[j][i]);
                        // OUT2(i, j) = atof(lines[j][i]);
                    }
                }
                """
        N = len(lines)
        x = len(lines[0])
        out = np.empty((x, N), dtype=np.float64)
        weave.inline(code, ['lines', 'N', 'x', 'out'])
        return out
    
    >>> matrix = [['0.5', '0.1', '0.7'],['0.2','0.2', '0.4']]
    >>> matrix
    [['0.5', '0.1', '0.7'], ['0.2', '0.2', '0.4']]
    >>> transpose(matrix)
    array([[ 0.5,  0.2],
           [ 0.1,  0.2],
           [ 0.7,  0.4]])
    

    Aside from constantly forgetting ; after something like 6 years without writing any C, I had a lot of trouble figuring out what out was turning into inside the C++ code, and in the end it is a pointer to the data itself, not to a PyArrayObject as the documentation states. There are two variables defined by weave that are available for use, out_array and py_out, which are of type PyArrayObject* and PyObject* respectively.

    I have left an alternative version of the assignment commented out: weave automatically defines macros <VAR>1, <VAR>2, <VAR>3, and <VAR>4 to access items of arrays of the corresponding number of dimensions.

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

Sidebar

Related Questions

A few wishes: If it is jQuery based, best. If it focuses on easy
Best to describe this in code... I have this public class A<T> { public
Best explained with code I think, this is just a simple example: public class
I'm starting in 3D programming. So I learn about vectors, matrices and all that
Can you recommend me a good book with code examples about Java secure network
I have been doing some testing whilst using Jquery UI datepicker and have found
I've got some troubles with OCMock and UIView. I have sort of this code:
I'm using the following code on ScraperWiki to search Twitter for a specific hashtag.
I need to code Java server and client using the local sockets. I know
I'm using the following code to generate a random string: <?php function random_string( )

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.