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

  • Home
  • SEARCH
  • 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 713447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:57:44+00:00 2026-05-14T04:57:44+00:00

Is there a smart and space-efficient symmetric matrix in numpy which automatically (and transparently)

  • 0

Is there a smart and space-efficient symmetric matrix in numpy which automatically (and transparently) fills the position at [j][i] when [i][j] is written to?

import numpy
a = numpy.symmetric((3, 3))
a[0][1] = 1
a[1][0] == a[0][1]
# True
print(a)
# [[0 1 0], [1 0 0], [0 0 0]]

assert numpy.all(a == a.T) # for any symmetric matrix

An automatic Hermitian would also be nice, although I won’t need that at the time of writing.

  • 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-05-14T04:57:44+00:00Added an answer on May 14, 2026 at 4:57 am

    If you can afford to symmetrize the matrix just before doing calculations, the following should be reasonably fast:

    def symmetrize(a):
        """
        Return a symmetrized version of NumPy array a.
    
        Values 0 are replaced by the array value at the symmetric
        position (with respect to the diagonal), i.e. if a_ij = 0,
        then the returned array a' is such that a'_ij = a_ji.
    
        Diagonal values are left untouched.
    
        a -- square NumPy array, such that a_ij = 0 or a_ji = 0, 
        for i != j.
        """
        return a + a.T - numpy.diag(a.diagonal())
    

    This works under reasonable assumptions (such as not doing both a[0, 1] = 42 and the contradictory a[1, 0] = 123 before running symmetrize).

    If you really need a transparent symmetrization, you might consider subclassing numpy.ndarray and simply redefining __setitem__:

    class SymNDArray(numpy.ndarray):
        """
        NumPy array subclass for symmetric matrices.
    
        A SymNDArray arr is such that doing arr[i,j] = value
        automatically does arr[j,i] = value, so that array
        updates remain symmetrical.
        """
    
        def __setitem__(self, (i, j), value):
            super(SymNDArray, self).__setitem__((i, j), value)                    
            super(SymNDArray, self).__setitem__((j, i), value)                    
    
    def symarray(input_array):
        """
        Return a symmetrized version of the array-like input_array.
    
        The returned array has class SymNDArray. Further assignments to the array
        are thus automatically symmetrized.
        """
        return symmetrize(numpy.asarray(input_array)).view(SymNDArray)
    
    # Example:
    a = symarray(numpy.zeros((3, 3)))
    a[0, 1] = 42
    print a  # a[1, 0] == 42 too!
    

    (or the equivalent with matrices instead of arrays, depending on your needs). This approach even handles more complicated assignments, like a[:, 1] = -1, which correctly sets a[1, :] elements.

    Note that Python 3 removed the possibility of writing def …(…, (i, j),…), so the code has to be slightly adapted before running with Python 3: def __setitem__(self, indexes, value): (i, j) = indexes…

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

Sidebar

Ask A Question

Stats

  • Questions 341k
  • Answers 341k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use lscape package with longtable or supertabular. May 14, 2026 at 4:58 am
  • Editorial Team
    Editorial Team added an answer I reimplemented the issue. Question became obsolete. May 14, 2026 at 4:58 am
  • Editorial Team
    Editorial Team added an answer To see what all $_SERVER contains, just do: echo "<pre>";… May 14, 2026 at 4:58 am

Related Questions

I was running some dynamic programming code (trying to brute-force disprove the Collatz conjecture
[Edit: This problem applies only to 32-bit systems. If your computer, your OS and
So I'm building the pacman game in Java to teach myself game programming. I
What is a fast and efficient way to implement the server-side component for an
I have a huge set of log lines and I need to parse each

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.