Coder Social home page Coder Social logo

Comments (4)

coreyostrove avatar coreyostrove commented on August 31, 2024

Sounds great!

from pygsti.

pcwysoc avatar pcwysoc commented on August 31, 2024

@rileyjmurray Could you explain a bit more when .ravel() can be used instead of .flatten()? I'm confused about when a copy is necessary.

from pygsti.

rileyjmurray avatar rileyjmurray commented on August 31, 2024

@pcwysoc, as a rule of thumb, copies aren't necessary. Python (or rather, numpy) makes them all the time. The main time when a copy would be important is if you're explicitly using in-place operations or if you're overwriting portions of an array.

import numpy as np

a = np.ones((3,5))
b = a.ravel()
b = b + 2
print(a) # unchanged, because the change to b1 wasn't specifically in-place.

b = a.ravel()
b += 2
print(a) # changed, since "+=" is an in-place operation

a = np.ones((3,5))
b = a.ravel()
b[::2] = 0.0  # overwrite every other element of b with 0.0.
print(a) # changed, since b shares memory with a

from pygsti.

rileyjmurray avatar rileyjmurray commented on August 31, 2024

I've started working on this.

Here are common patterns I've seen where .flatten() can be safely replaced with .ravel():

  • np.concatenate([ ... vec.flatten() ... ]), since the concatenation will allocate new memory anyway.
  • f(vec.flatten()) where f the return value of f is guaranteed not to point to its argument's memory. For example, f could be np.all, or np.sum, or np.isnan.
  • y = f(x).flatten() where f(x) returns a new array

I'll edit this comment as I go.

from pygsti.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.