Exemple #1
0
    def _toSLH(self):

        # These numerically optimal solutions were obtained as outlined in
        # my blog post on the Mabuchi-Lab internal blog
        # email me ([email protected])for details.
        if self.N == 1:
            kappa0 = 9.28874141848 / self.tau
            kappas = np_array([7.35562929]) / self.tau
            Deltas = np_array([3.50876192]) / self.tau
        elif self.N == 3:
            kappa0 = 14.5869543803 / self.tau
            kappas = np_array([ 13.40782559, 9.29869721]) / self.tau
            Deltas = np_array([3.48532283, 7.14204585]) / self.tau
        elif self.N == 5:
            kappa0 = 19.8871474779 / self.tau
            kappas = np_array([19.03316217, 10.74270752, 16.28055664]) / self.tau
            Deltas = np_array([3.47857213, 10.84138821, 7.03434809]) / self.tau
        else:
            raise NotImplementedError("The number of cavities to realize the delay must be one of 1,3 or 5.")

        h0 = make_namespace_string(self.name, 'C0')
        hp =  [make_namespace_string(self.name, "C{:d}p".format(n+1)) for n in range((self.N-1)/2)]
        hm =  [make_namespace_string(self.name, "C{:d}m".format(n+1)) for n in range((self.N-1)/2)]


        S = Matrix([1.])
        slh0 = SLH(S, Matrix([[sqrt(kappa0) * Destroy(h0)]]), ZeroOperator)
        slhp = [SLH(S, Matrix([[sqrt(kj) * Destroy(hj)]]), Dj * Create(hj) * Destroy(hj)) for (kj, Dj, hj) in zip(kappas, Deltas, hp)]
        slhm = [SLH(S, Matrix([[sqrt(kj) * Destroy(hj)]]), -Dj * Create(hj) * Destroy(hj)) for (kj, Dj, hj) in zip(kappas, Deltas, hm)]

        return freduce(lambda a, b: a << b, slhp + slhm, slh0)
Exemple #2
0
def accum_test(accum_gear, reduce_func):
    init = [7, 45]
    seq = [list(range(0, 100, 10)), list(range(2))]

    directed(drv(t=Queue[Uint[8]], seq=seq),
             drv(t=Uint[8], seq=init),
             f=accum_gear,
             ref=[freduce(reduce_func, s, i) for s, i in zip(seq, init)])

    sim()
Exemple #3
0
def test_uint_directed(sim_cls):
    init = [7, 45]
    seq = [list(range(0, 100, 10)), list(range(2))]

    def add(x, y):
        return saturate(x + y, Uint[8])

    directed(drv(t=Queue[Uint[8]], seq=seq),
             drv(t=Uint[8], seq=init),
             f=reduce(f=add, sim_cls=sim_cls),
             ref=[freduce(add, s, i) for s, i in zip(seq, init)])
    sim()
Exemple #4
0
    def fprod(iterable, start=_1_0):
        '''Iterable product, like C{math.prod} or C{numpy.prod}.

           @arg iterable: Terms to be multiplied (C{scalar}[]).
           @kwarg start: Initial term, also the value returned
                         for an empty iterable (C{scalar}).

           @return: The product (C{float}).

           @see: U{NumPy.prod<https://docs.SciPy.org/doc/
                 numpy/reference/generated/numpy.prod.html>}.
        '''
        return freduce(_mul, iterable, start)
Exemple #5
0
def fprod(iterable, start=1.0):
    '''Iterable product, like C{numpy.prod}.

       @param iterable: Values to be multiplied (C{scalar}[]).
       @keyword start: Initial product, also the value returned
                       for an empty iterable (C{scalar}).

       @return: The product (C{float}).

       @see: U{NumPy.prod<http://docs.SciPy.org/doc/
             numpy/reference/generated/numpy.prod.html>}.
    '''
    return freduce(mul, iterable, start)
def _maybe_log_technical_terms(global_options, tool_options):
    """Log technical terms as appropriate if the user requested it.

    As a side effect, if --log-technical-terms-to is passed to the linter
    then open up the file specified (or create it) and then merge the set
    of technical words that we have now with the technical words already
    in it.
    """
    log_technical_terms_to_path = global_options.get("log_technical_terms_to",
                                                     None)
    log_technical_terms_to_queue = tool_options.get("log_technical_terms_to",
                                                    None)
    if log_technical_terms_to_path:
        assert log_technical_terms_to_queue is not None

        try:
            os.makedirs(os.path.dirname(log_technical_terms_to_path))
        except OSError as error:
            if error.errno != errno.EEXIST:
                raise error

        if not log_technical_terms_to_queue.empty():
            with closing(os.fdopen(os.open(log_technical_terms_to_path,
                                           os.O_RDWR | os.O_CREAT),
                                   "r+")) as terms_file:
                # pychecker can't see through the handle returned by closing
                # so we need to suppress these warnings.
                terms = set(terms_file.read().splitlines())
                new_terms = set(freduce(lambda x, y: x | y,
                                        _drain(log_technical_terms_to_queue)))

                if not terms.issuperset(new_terms):
                    terms_file.seek(0)
                    terms_file.truncate(0)
                    terms_file.write("\n".join(list(terms |
                                                    set(new_terms))))
Exemple #7
0
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from functools import reduce as freduce

NOTES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']

_freqs4 = [261.63, 277.18, 293.66, 311.13, 329.63, 349.23, 369.99, 392.0, 415.3, 440.0, 466.16, 493.88]
FREQS = freduce(lambda l1, l2: l1+l2, ([ freq/2**i for freq in _freqs4 ] for i in range(4, -4, -1)))

def find_freqnotecents(f):
    freq = min(FREQS, key=lambda i: abs(i-f))
    idx = FREQS.index(freq)
    octave = int(idx / 12)
    note = NOTES[FREQS.index(freq/2**octave)]
    d = 1 if f >= freq else -1
    next_f = FREQS[idx+d]
    cents =  int(100 * (f - freq) / (next_f - freq)) * d
    return freq, "%s%d" % (note, octave), cents