Multiset domination algorithm

Let us say that a multiset M _dominates_ another multiset N if each element in N occurs at least as many times in M.

Given a target multiset M and an integer k>0, I'd like to find a list, L, of size-k multisets whose sum dominates M. I'd like this list to be of small cost, where my cost function is of the form:

cost = c*m + n

where c is a constant, m is the number of multisets in L, and n is the number of _distinct_ multisets in L.

How can I do this? An efficient algorithm to find the optimal solution would be ideal.

The problem comes from trying to fulfill a customer's order for printing pages with a specialized block-printer that prints k pages at a time. Setting up the block-printer to print a particular template of k pages is costly, but once a template is initialized, printing with it is cheap. The target multiset M represents the customer's order, and the n distinct multisets of the list L represent n distinct k-page templates.

In my particular application, M typically has >30 elements whose multiplicities are in the range [10^4, 10^6]. The value of k is 15, and c is approximately 10^-5.

Well given that k is 15 and your example is worst case for my approach it can be bearable IMO. At least it gives you a fixed cap, with the "one of a kind" approach that would be optimal in your example, if distinct elements aren't multiples of k or multiplicities vary a lot (they have a 10^2 range right?) you're back having to find a way of grouping them. You can always take some initial time calculating the number of distinct elements and their multiplicity, then based on that remove as many as you can with you "horizontal" approach and clean up the rest with "vertical" approach.

@CarloMoretti Consider the case when M consists of a*k distinct elements, each with identical multiplicity b. Clearly the optimal cost here is a(cb+1). Your solution, if I understand it correctly, does no better than a(cb+k). This performs nearly k times worse than optimal if k>>cb.

Since multiplicities are so high why not just make sets of k identical elements? Let's say M has p distinct elements, you'll have at first n = p (without counting the reminder multiplicities). Since k is 15 I guess all combined remaining items will be r < 15*p which if you combine them at random will result in < p k-sets. So at the end you'll have n < 2p. So m = |M| / 15 if p is [10, 10^2] then |M| is in [10^5, 10^8] and m is in [10^4, 10^7] so c*m would be in [10^-1, 10^2] which is fairly comparable to p so roughly cost < 3p

This seems like a really interesting question! It does seem like it might be NP-hard to find the exact optimum, but I would be very interested in hearing some sample problem instances to see what I can get.

@DavidEisenstat btw, I just deleted an earlier comment saying I could express the optimization problem with linear constraints, but I realized I was wrong. The only formulation I know of currently has quadratic constraints.

@piotrekg2 My friend has so far only given me one problem instance, without a corresponding human solution. I am pushing him to make more available.

@dshin I know about the magic of commercial solvers, but I'd be a little surprised if you had a formulation that they would work well on.

@dshin Did you try to compare combinations computed by humans with combinations computed by a greedy algorithm?

@DavidEisenstat I don't think integer quadratic programs are optimally solvable in polynomial time in general, but that often doesn't stop commercial scientific solvers from doing a good job. Adding a restriction of not sharing pages between templates hurts your ability to minimize cost.

(Fixed prev comment) This is similar to the NP-hard Cutting Stock problem. I've come up with a deterministic algorithm that will take just O(k|M|^2) total time (with |M| being the number of elements in M, not their sum) to find |M| solutions: one for each possible number i of distinct templates. The solution for i distinct templates exactly minimises the maximum copy count of any template under the constraint that it is possible to order templates, and pages, so that each page spans a contiguous block of templates. Taking the best of these |M| solutions should give a good quality answer.

@j_random_hacker I was actually introduced to this problem by my friend - he told me that his company's current solution is to have a human try combinations by hand for hours until he finds one that seems ok. If you are serious about being paid, I can get you guys in touch. I'd imagine he'd want to see how the algorithm compares to the human solution (and to a simple greedy solution) on real life examples to put a dollar amount on the algorithm's value.

Interesting problem! Is the quadratic program actually solvable to optimality? Would it make sense to consider a variant where each type of page can belong to exactly one template?

@dwanderson One approach is to fix n and then formulate the optimization problem as an integer quadratic program, which can be solved with a scientific library. Then iterate over candidate values of n. I'm hoping for something nicer. I've tried a greedy-ish algorithm which works decently but is not optimal.

My guess is that finding the optimal solution is NP-complete, but greedy can find a solution fairly easily.

What have you tried? SO isn't a code-writing service. Which parts of the algorithm confuse you or are you having trouble with?


Equality of int&'s in template parameters

Suppose we have the following program:

    template <class T, T n1, T n2>
    struct Probe {
        static const int which = 1;
    };

    template <class T, T n>
    struct Probe<T, n, n> {
        static const int which = 2;
    };

    int i = 123;

    const int myQuestion = Probe<int&, i, i>::which;

I am pretty sure, that `myQuestion` should be `2` regardless of the version of C++ standard, but compilers disagree upon it. MSVC and clang say that it is `2` until C++14, and `1` since C++17. See the [demo](https://godbolt.org/z/efETKzTo3). What is the truth?

My investigation so far:

  * I have found one relevant sentence in the C++ standard. It was there in [C++11](https://timsong-cpp.github.io/cppwp/n3337/temp.type#1.6), [C++14](https://timsong-cpp.github.io/cppwp/n4140/temp.type#1.6), [C++17](https://timsong-cpp.github.io/cppwp/n4659/temp.type#1.6) and [C++20](https://timsong-cpp.github.io/cppwp/n4868/temp.type#2.7). It did not change.
  * If you remove the parameter `T` from the example code, all compilers agree, that `myQuestion` is `2`. [Demo](https://godbolt.org/z/6cW8GGEod).

Ok, I don't have an issue with that. It is a duplicate, but it's not exactly a question that gets asked a lot, so it's not a big deal to leave it open. I wouldn't dispute the closure either, i.e. I'm fine not voting either way.

@cigien: It's not a good idea to mark my question as a duplicate of that long question. Even if my question is answered there, since it's so long, it would be difficult for the readers to extract information from that.

@AnoopRana - Accessing the object's value in a constant expression requires it to satisfy additional constraints so the value is usable (in that case it should be constexpr indeed). Referring to the object alone (like in the OP, on surface level) doesn't require anything from the value.

This is a duplicate of stackoverflow.com/questions/37369129 but can't be targetted because it doesn't have an upvoted/accepted answer.

@StoryTeller-UnslanderMonica Ok then shouldn't this work without error? As you said, here we have a reference ref to an object i with static storage duration. Then ref can be used as size of an array.  Am i missing something?

@AnoopRana - timsong-cpp.github.io/cppwp/n4868/expr.const#11 should have the gist of it.

@StoryTeller-UnslanderMonica I think you're right[in which case i'll delete my comments] but i can't find the exact statement that you quoted: "A reference to an object with static storage duration is permissable in constant expressions, even when the referent is not const". Can you put a link here for me?

@AnoopRana - And you missed the point of mine. It already is valid in constant expressions.

@AnoopRana Same error with contexpr/const int&: godbolt.org/z/a5Mrj4M53

@AnoopRana - it's not ill-formed. For god's sake, it's a minimal reproducible example, check it before commenting. A reference to an object with static storage duration is permissable in constant expressions, even when the referent is not const.

Definitely an interesting Q. References are meant to be translucent in the type system.  Not sure how that may interplay with template equivalence checks however.

Looks like the program is ill-formed since i is not a constant expression in your case. If you make it a constant expression, the program will no longer compile in any version.

First i should be a constant expression when passing as template argument. Add constexpr or const for it.


Can skew binomial heaps support efficient merge?

The skew binomial heaps described in Okasaki's _Purely Functional Data Structures_ support merge in worst-case `O(log (max (m,n)))` time, where `m` and `n` are the lengths of the queues being merged. This is worse than segmented binomial queues, which support it in worst-case `O(log (min (m,n)))` time, and lazy binomial queues, which support it in worst-case `O(log (max (m,n)))` time but `O(log (min (m,n)))` amortized time[*]. This seems to be inherent in the restriction that the skew binary number in the queue representation is in canonical form (only one 2, and only as the least significant nonzero digit). Would it be possible to relax this restriction somewhat to get more efficient merges? The basic challenge is that a 2 must not be allowed to cascade into another 2.

[*] I've also recently come up with a variant of scheduled binomial queues with the same worst-case bounds as segmented queues; that version is not yet fully implemented.


Using ESM Modules with Coffeescript and Node.js

Ecmascript Modules are the future for packaging JS code, and both Node.js and Coffeescript support them. But I've had some trouble getting their support for ESM to work together.

The current stable Node (12.x) has ESM modules behind a flag (`--experimental-modules`). Coffeescript supports passing flags through to Node with `--nodejs`. So with a couple of files using ESM modules:

    # a.coffee
    import b from './b.coffee'

    b()

    # b.coffee
    b = ->
      console.log "Hello"

    export default b

In theory, we can run this code with `npx coffee --nodejs --experimental-modules a.coffee`. In practice this raises an error:

    13:24 $ npx coffee --nodejs --experimental-modules a.coffee
    (node:8923) ExperimentalWarning: The ESM module loader is experimental.
    (node:8923) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
    /media/projects/coffeemodules/a.coffee:1
    import b from './b.coffee';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module
    ...

The error and docs say there are two ways to flag a file as containing an ESM module, one is to use the `mjs` extension (which isn't available to us here), and the other is to set `"type": "module"` in `package.json`, which also doesn't seem to work.

So: can it be done? Is there a way to get Coffeescript and Node.js ES modules to play together?


MSDeploy not replacing encoded xml strings

In web.config I have:

    <applicationSettings>
    <App.Properties.Settings>
        <setting name="ProfitConnectorToken" serializeAs="String" xdt:Transform="Replace" xdt:Locator="Match(name)">
            <value>__ProfitConnectorToken__</value>
        </setting>
    </App.Properties.Settings>

In my parameters.xml:

    <parameter name="ProfitConnectorToken" description="Description for ProfitConnectorToken" defaultvalue="__PROFITCONNECTORTOKEN__" tags="">
    <parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/applicationSettings/App.Properties.Settings/setting[@name='ProfitConnectorToken']/value/text()" />

And in my SetParameters.xml:

    <setParameter name="ProfitConnectorToken" value="<token><version>1</version><data>XXXXXXXXXXXXXXXXXXXXXXXXX</data></token>" />

But this value is not set when the web application is deployed. When I change my SetParameters.xml to:

    <setParameter name="ProfitConnectorToken" value="TEST" />

It does work, so my XPath is correct. Why is the encoded xml value not set?

Nope, if I do that the value isn't replaced at all, not even my "TEST" value which was working before.

Try removing /text() at the end of your XPath query?


How to ignore warnings in headers that are part of a clang C++ module?

We're using clang with `-fmodule` and `-fcxx-module` to enable module support as documented at <http://clang.llvm.org/docs/Modules.html>. We're already seeing a significant improvement in build times by defining module maps for our core libraries.

However, we have some library headers that use pragmas to disable warnings for certain lines, for example:

    template <typename TFloat>
    static bool exactlyEqual(TFloat lhs, TFloat rhs)
    {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wfloat-equal"
        return lhs == rhs;
    #pragma clang diagnostic pop
    }

When this header is pulled in as a precompiled module, it seems clang's internal representation does not preserve the pragma information and the warning is still emitted. Since we treat warnings as errors this causes compilation to fail. Some might argue to just disable `float-equal` entirely, but we have a bunch of other cases with different warnings which we don't want to globally disable.

We're already using `-Wno-system-headers` and `-isystem` so that clients of libraries generally don't see warnings like this anyway (even without the pragma), but this doesn't seem to work when the header is imported as a module. In addition we still hit these warnings for code internal to the library which includes the header as a non-system header (i.e. without using `-isystem` / using double quotes), since module precompilation and importing also occurs here.

I've tried using `_Pragma(...)` instead of `#pragma` which didn't have any effect.

Is there some other way to conditionally ignore warnings in headers that come from precompiled clang modules?

**UPDATE:** I've put a sample project up on <https://github.com/MikeWeller/ClangModuleWarnings> which reproduces the problem

**UPDATE:** Seems the `[system]` module attribute will suppress all warnings. However this suppresses even warnings we want to see when building the library itself, and we don't want to make all our modules system modules. If we find a way to not use the library's module map when building the library itself this may be acceptable but we'd still like to pragma out certain warnings for non-system modules..

Seeing as this question was asked way back when in 2016, are you still having this issue? Have you asked on the Clang mailing list?


iOS OpenGL Catch-22: OpenGL background rules and "app snapshot" for App Switcher

Like many developers, I have an app that uses OpenGL via a `UIView` subclass whose `layerClass:` method returns `[CAEAGLLayer class]`.

Note I am **not** using `GLKit` or `GLKView` or `GLKViewController`

When I click Home to put the app into the background, after `applicationDidEnterBackground`, iOS calls my view's `layoutSubviews` twice, with portrait and landscape sizes, trying to generate an "app snapshot" as explained here (see "prepare for the app snapshot"):

<https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html#//apple_ref/doc/uid/TP40007072-CH8-SW10>

How can this possibly work?

There seems to be a direct contradiction here with the very clear advice on this page (see "Background Apps May Not Execute Commands on the Graphics Hardware"):

<https://developer.apple.com/library/ios/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/ImplementingaMultitasking-awareOpenGLESApplication/ImplementingaMultitasking-awareOpenGLESApplication.html#//apple_ref/doc/uid/TP40008793-CH5-SW1>

that we must not draw anything with OpenGL after `applicationDidEnterBackground`

If we don't draw, we cannot generate the snapshots. We must violate one rule or the other.

But we also want good snapshots in both orientations, so that when the user double-clicks home and goes to the App Switcher, they see reasonable snapshot images.

Even if I temporarily change my code to fully implement the `layoutSubviews` after `applicationDidEnterBackground` by creating an OpenGL surface and drawing (which, contrary to the Apple dox, does not crash), and then I double-click home and look at the snapshot in different orientations, only the snapshot for the orientation I was in before is correct. The other one is a super-ugly nasty re-scaling of the other snapshot. Apple seems to be going through the motions of taking snapshots, but not actually taking them.

I am seeing this behavior on iOS 9.3.2 on an iPad Mini. The behavior doesn't show on most/all iPhone devices since they don't support a landscape App Switcher.

**UPDATE:** the problem also happens, and happens much worse, when using the new iOS 9 "Slide Over" multitasking feature and switching the same app between being a normal fullscreen app vs. being an app slid over another app. iOS only seems to capture a snapshot of the last app size, so after using the app at 640px wide and then trying to use the App Switcher to get to the app fullscreen, we see a grotesque pixely out-of-proportion snapshot in the App Switcher and also during the first second of launch. There has got to be some way to fix this!

**UPDATE 2:** I have seen a few iOS apps, which I know to be OpenGL-only apps, where if you use them in portrait, then go back Home and rotate to landscape, then double-click Home, you see the portrait launch image rather than a horrible, distorted, out-of-proportion image like I am seeing. While I would prefer to render snapshot images, I would even be happy to see the launch image. But the option everyone mentions, `ignoreSnapshotOnNextApplicationLaunch`, **does not work** because it only affects what you see at actual app launch time, not what is seen in the App Switcher when you double-click Home, and for many on StackOverflow it actually didn't even work at all (not even at launch time).

How do we get around this Catch-22?

This StackOverflow thread (unlike me, the OP here uses GLKit but the symptom is the same):

[iOS OpenGL ES screen rotation while background apps bar visible](https://stackoverflow.com/questions/13954994/ios-opengl-es-screen-rotation-while-background-apps-bar-visible)

confirms that some OpenGL apps on iOS **are** able to have proper preview images in the Home double-press app switcher for both orientations. How do they do it?

How can I get proper snapshots shown in both orientations in the App Switcher?

Here is a log of `AppDelegate` (appdel), `ViewController` (eaglc) and `View` (eaglv) calls that come from iOS at the time that I click the Home button once to exit the app. You can see the attempts at snapshotting that come well after `didEnterBackground`:

    +  189.57ms      appdel appWillResignActive
    +    0.74ms        appdel appWillResignActive between_view_os_callbacks 0
    +    4.11ms        appdel appWillResignActive between_view_os_callbacks 0 done
    +    0.82ms        appdel appWillResignActive activation_changed
    +    1.50ms        appdel appWillResignActive activation_changed done
    +    0.47ms        appdel appWillResignActive between_view_os_callbacks 1
    +    2.68ms          drawing rect [(144,1418)+(2,66)] (0 left)
    +   44.28ms          swap_buffers glFlush()
    +    6.16ms          swap_buffers presentRenderBuffer
    +    9.01ms        appdel appWillResignActive between_view_os_callbacks 1 done
    +    0.61ms        appdel save_state
                         ..app saving data, no OpenGL here..
    +    0.49ms          appdel save_state calling glFinish
    +    0.34ms        appdel save_state done
    +    0.25ms      appdel appWillResignActive done
    +  492.72ms      appdel applicationDidEnterBackground
    +    0.56ms        appdel save_state
                         ..app saving data, no OpenGL here..
    +    0.65ms          appdel save_state calling glFinish
    +    0.54ms        appdel save_state done
    +    0.65ms        eaglv let_go_of_frame_buffer_render_buffer
                         app drops OpenGL frame_buffer and render_buffer here
    +    1.10ms      appdel applicationDidEnterBackground done

    Now we are not supposed to do OpenGL, BUT...

    +    6.30ms      eaglc supportedInterfaceOrientations
    +    5.74ms      about_to_sleep between_view_os_callbacks
    +    1.30ms        SKIPPING between_view_os_callbacks cuz app in background
    +    0.66ms      about_to_sleep between_view_os_callbacks done
    +  135.85ms      eaglc willRotateToInterfaceOrientation
    +    2.49ms      appdel willChangeStatusBarFrame new=0,0 768x20
    +    3.21ms      appdel didChangeStatusBarFrame old=0,0 1024x20

    we get a portrait layoutSubviews....

    +    1.26ms      eaglv layoutSubviews (initted=1, have_fbrb=0)
    +    1.80ms        eaglv assure_frame_buffer_render_buffer
    +    0.95ms          eaglv assure_fbrb scale ios=2 eaglv=2
    +    0.90ms          eaglv assure_fbrb (frame=1536,2048)
    +    1.04ms          eaglv assure_fbrb (layer frame=1536,2048)
    +    0.92ms        eaglv assure_fbrb in bg: will make fbrb later
    +    0.96ms      eaglv layoutSubviews done
    +    3.11ms      eaglc didRotateFromInterfaceOrientation
    +  149.07ms      eaglc willRotateToInterfaceOrientation
    +    1.99ms      appdel willChangeStatusBarFrame new=0,0 1024x20
    +    2.35ms      appdel didChangeStatusBarFrame old=0,0 768x20

    then a landscape layoutSubviews...

    +    1.91ms      eaglv layoutSubviews (initted=1, have_fbrb=0)
    +    1.09ms        eaglv assure_frame_buffer_render_buffer
    +    0.91ms          eaglv assure_fbrb scale ios=2 eaglv=2
    +    1.65ms          eaglv assure_fbrb (frame=2048,1536)
    +    0.92ms          eaglv assure_fbrb (layer frame=2048,1536)
    +    0.93ms        eaglv assure_fbrb in bg: will make fbrb later
    +    0.83ms      eaglv layoutSubviews done
    +    2.79ms      eaglc didRotateFromInterfaceOrientation

    and, adding insult to injury, we get this log message:

    Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

perhaps you can try to allow only portrait mode when entering background, so that it at least wouldn't generate distorted snapshot ... but i think this is a good question, and hope somebody else could offer some insights

I do update my UI before returning from applicationDidEnterBackground, however the problem is that the two layoutSubviews calls which represent my only chance to draw the state of the app in the other orientation (portrait or landscape, whichever one it is) both come AFTER applicationDidEnterBackground, when Apple says it is forbidden to draw.  Catch-22!  So even if Apple were using my app state that I leave after applicationDidEnterBackground, it's not possible that Apple will know how to draw the app in the other orientation.  And so I see distorted ugliness in the App Switcher.

Are you updating your UI / drawing with GLES in layoutSubviews:? Apple states that you need to do that "Before returning from your applicationDidEnterBackground: method", which means you need to do UI updates in applicationDidEnterBackground:


How to calculate the checksum in an XFA form

When you save an XFA form (XFA = XML Forms Architecture) using Adobe software, a checksum attribute is added to the form element. This checksum appears to be a SHA-1 digest, but it's unclear as to what is actually fed to the hash. Does anyone have any idea as to how this is generated? This value is needed by Adobe Acrobat to validate what's actually in the form's XML data, but when I create a hash of the XML that is being fed to the form, Adobe Acrobat doesn't accept it. This checksum attribute isn't documented in the XFA specification, so I would really appreciate it, if somebody could:

  1. Confirm that the value is actually a hash created using the SHA-1 hashing algorithm?
  2. Explain which data should be used to create this hash.

It seems like iText once had an implementation of this, but I can't find it in the more recent versions of their software: api.itextpdf.com/pdfXFA/java/2.0.2/index.html?com/itextpdf/tool/…

@BrunoLowagie I am working on populate data into "form" - xfa.org/schema/xfa-form/2.8. But it doesn't affect any. So, without checksum, we cannot do it?

@Setasign I don't have any answer yet. Foxit must have reverse engineered it...

Bruno, do you have any news on that issue? Or is it still in the hands of Adobe? Foxit, for example, is able to create this hash...


Listings package and forcing inclusion of empty lines at end of line ranges?

By default, the listings package suppresses empty lines at the end of the file, and this behavior can be switched off with the option `showlines`.

However, listings also suppresses empty lines at the end of each range in a `linerange`. Consider the following Java source file:

    public class Example implements StringHandler {

        /**
         * Prints the given string.
         *
         * @param s  the given string
         */
        @Override
        public void handle(String s) {
            System.out.println(s);
        }

    }

And this is the LaTeX source file `test.tex`:

    \documentclass[a4paper,landscape]{slides}
    \usepackage{color,listings,courier}

    \lstset{language=Java,%
      basicstyle=\ttfamily,%
      numbers=left,%
      numberstyle=\tiny,%
      commentstyle=\color{blue}\itshape}

    \begin{document}

    % Suppress complete javadoc and @Override (to avoid cluttering the slide)
    \begin{slide}
    \lstinputlisting[linerange={1-2,9-13}]{Example.java}
    \end{slide}

    \end{document}

The expected output is:

    public class Example implements StringHandler {

        public void handle(String s) {
            System.out.println(s);
        }

    }

But instead, I get

    public class Example implements StringHandler {
        public void handle(String s) {
            System.out.println(s);
        }

    }

Line 2 (which is empty) is not included in the output, being at the end of a line range. (I am using listings v1.5; but v1.3 behaves similarly.)

It turns out that empty lines at the _beginning_ of a line range (consisting of more than one line) are included. But that is of no help in this situation.

How can I force the inclusion of these embedded empty lines? This is useful to make the listing more readable.

The options `showlines` and `emptylines` do not affect this behavior.

@user2768: No, I never a solution.
Did you ever find a solution?


Glossary style with tabularray

I am currently migrating from xltabular to tabularray. As part of that I want to adapt my custom glossary style for List of Symbols and List of Abbreviations (same style).

This is my old code:

    % tables
    \usepackage{booktabs}
    \usepackage{multirow}
    \usepackage{xltabular}
    \usepackage{tabularray}
    \addto\captionsngerman{
        \DefTblrTemplate{contfoot-text}{default}{Fortsetzung auf der n\"achsten Seite}
        \DefTblrTemplate{conthead-text}{default}{(fortgesetzt)}
    }

    % glossaries
    \usepackage[
        abbreviations,
        nonumberlist,
        record,
        symbols
    ]{glossaries-extra}
    \GlsXtrLoadResources[
        src=glossaries,
        not-match={entrytype=symbol}
    ]
    \GlsXtrLoadResources[
        selection=all,
        src=glossaries,
        type=symbols,
        match={entrytype=symbol}
    ]
    \newglossarystyle{customlong}{
        \setglossarystyle{long}
        \renewenvironment{theglossary}{\xltabular{\textwidth}{llX}}{\endxltabular}
        \renewcommand{\glossentry}[2]{
            \glsentryitem{##1}\textbf{\glstarget{##1}{\glossentryname{##1}}} & \multicolumn{2}{X}{\glossentrydesc{##1}}\\
        }
        \renewcommand{\subglossentry}[3]{
            & \glssubentryitem{##2}\glstarget{##2}{\strut} & \glossentrydesc{##2}\\
        }
        \ifglsnogroupskip
            \renewcommand*{\glsgroupskip}{}%
        \else
            \renewcommand*{\glsgroupskip}{\\}%
        \fi
    }

I would like to get rid of:

    \usepackage{booktabs}
    \usepackage{multirow}
    \usepackage{xltabular}

Therefore, I need to adapt `\newglossarystyle{customlong}{...}`. This is my current state:

    \newglossarystyle{customlong}{
        \setglossarystyle{long}
        \renewenvironment{theglossary}{
            \begin{longtblr}[
                entry=none,
                label=none
            ]{
                colspec={llX},
                hspan=minimal,
                stretch=1,
                width=\textwidth
            }
        }{\end{longtblr}}
        \renewcommand{\glossentry}[2]{
            \glsentryitem{##1}\textbf{\glstarget{##1}{\glossentryname{##1}}} & \SetCell[c=2]{l}\glossentrydesc{##1}\\
        }
        \renewcommand{\subglossentry}[3]{
            & \glssubentryitem{##2}\glstarget{##2}{\strut} & \glossentrydesc{##2}\\
        }
        \ifglsnogroupskip
            \renewcommand*{\glsgroupskip}{}%
        \else
            \renewcommand*{\glsgroupskip}{\\}%
        \fi
    }

Compilation fails with 'Misplaced alignment tab character &.'. When I replace `&` with `\&` in `\renewcommand{\glossentry}[2]{...}` and `\renewcommand{\subglossentry}[3]{...}`, the compilation is successful but the output obviously contains '&'.

I assume that this error is related to the following content of the package documentation of tabularray:

> "In contrast to traditional tabular environment, tabularray environments need to see every & and \ when splitting the table body with l3regex. And you can not put cell text inside any table command defined with \NewTableCommand. But you could use outer key expand to make tabularray expand every occurrence of a specified macro once before splitting the table body. Note that you can not expand a command defined with \NewDocumentCommand." (<https://ftp.gwdg.de/pub/ctan/macros/latex/contrib/tabularray/tabularray.pdf>, p. 30)

This would mean that I need to expand `\renewcommand{\glossentry}[2]{...}` and `\renewcommand{\subglossentry}[3]{...}`. I have no idea how to achieve that. Can someone help?

Thanks in advance.

# Edit 1: M(N)WE

This is my current code which prints '&'. Some unrelated contents have been removed. I build with `latexmk`.

## main.tex

    \documentclass{scrbook}

    \usepackage{tabularray}

    \usepackage[
        abbreviations,
        nonumberlist,
        record,
        symbols
    ]{glossaries-extra}
    \GlsXtrLoadResources[
        src=glossaries,
        not-match={entrytype=symbol}
    ]
    \GlsXtrLoadResources[
        selection=all,
        src=glossaries,
        type=symbols,
        match={entrytype=symbol}
    ]
    \newglossarystyle{customlong}{
        \setglossarystyle{long}
        \renewenvironment{theglossary}{
            \begin{longtblr}[
                entry=none,
                label=none
            ]{
                colspec={llX},
                hspan=minimal,
                stretch=1,
                width=\textwidth
            }
        }{\end{longtblr}}
        \renewcommand{\glossentry}[2]{
            \glsentryitem{##1}\textbf{\glstarget{##1}{\glossentryname{##1}}} \& \SetCell[c=2]{l}\glossentrydesc{##1}\\
        }
        \renewcommand{\subglossentry}[3]{
            \& \glssubentryitem{##2}\glstarget{##2}{\strut} \& \glossentrydesc{##2}\\
        }
        \ifglsnogroupskip
            \renewcommand*{\glsgroupskip}{}%
        \else
            \renewcommand*{\glsgroupskip}{\\}%
        \fi
    }
    \newglossarystyle{customindex}{
        \setglossarystyle{index}
        \renewcommand{\glstreeitem}{\parindent0pt\par}
        \renewcommand{\glstreepredesc}{\par\glstreeitem\parindent40pt\hangindent40pt}
    }

    \begin{document}
        \frontmatter
            \printunsrtabbreviations[style=customlong]
            \printunsrtsymbols[style=customlong]

        \mainmatter

        \appendix

        \backmatter
                \printunsrtglossary[style=customindex]
    \end{document}

## glossaries.bib

    @entry{gls-uml,
        name        = {Unified Modeling Language},
        description = {\enquote{A specification defining a graphical language for visualizing, specifying, constructing, and documenting the artifacts of distributed object systems.}\footnote{\url{https://www.omg.org/spec/UML}, aufgerufen am 20.02.2023}.}
    }

    @abbreviation{auv,
        description = {\gls{gls-auv}},
        short       = {AUV},
        long        = {Autonomous Underwater Vehicle}
    }

    @symbol{v-desired,
        name        = {\ensuremath{\overrightarrow{v_{desired}}}},
        description = {Wunschgeschwindigkeit}
    }

## .latexmkrc

    @default_files = ('main');

    $pdf_mode = 4;
    $dvi_mode = 0;
    $postscript_mode = 0;

    $lualatex = 'lualatex -synctex=1 -interaction=nonstopmode %O %S';

    push @generated_exts, 'glstex', 'glg';

    $clean_ext .= ' %R.bbl %R.glstex %R.lol %R.run.xml %R-1.glstex ';

    add_cus_dep( 'aux', 'glstex', 0, 'run_bib2gls' );

    sub run_bib2gls {
            if ($silent) {
                    my $ret = system "bib2gls --silent --group $_[0]";
            }
            else {
                    my $ret = system "bib2gls --group $_[0]";
            }

            my ( $base, $path ) = fileparse( $_[0] );
            if ( $path && -e "$base.glstex" ) {
                    rename "$base.glstex", "$path$base.glstex";
            }

            # Analyze log file.
            local *LOG;
            $LOG = "$_[0].glg";
            if ( !$ret && -e $LOG ) {
                    open LOG, "<$LOG";
                    while (<LOG>) {
                            if (/^Reading (.*\.bib)\s$/) {
                                    rdb_ensure_file( $rule, $1 );
                            }
                    }
                    close LOG;
            }
            return $ret;
    }

I am having the same Problem... I think Nicola Talbot could say if glossaries ist able to handle this or not.

Welcome to TeX.SX! Can you please add a complete minimal working example starting with \documentclass and ending with \end{document} instead of showing only some code snippets. This would help us to test you code, reproduce the issue and find and test a suggestion.


Is the Bellman equation that uses sampling weighted by the Q values (instead of max) a contraction?

It is proved that the Bellman update is a contraction (1).

**Here is the Bellman update that is used for Q-Learning:**

$$Q_{t+1}(s, a) = Q_{t}(s, a) + \alpha*(r(s, a, s') + \gamma \max_{a^*} (Q_{t}(s', a^*)) - Q_t(s,a)) \tag{1} \label{1}$$

The proof of (\ref{1}) being contraction comes from one of the facts (the relevant one for the question) that max operation is non expansive; that is:

$$\lvert \max_a f(a)- \max_a g(a) \rvert \leq \max_a \lvert f(a) - g(a) \rvert \tag{2}\label{2}$$

This is also proved in a lot of places and it is pretty intuitive.

**Consider the following Bellman update:**

$$ Q_{t+1}(s, a) = Q_{t}(s, a) + \alpha*(r(s, a, s') + \gamma SAMPLE_{a^*} (Q_{t}(s', a^*)) - Q_t(s,a)) \tag{3}\label{3}$$

where $SAMPLE_a(Q(s, a))$ samples an action with respect to the Q values (weighted by their Q values) of each action in that state.

**Is this new Bellman operation still a contraction?**

Is the SAMPLE operation non-expansive? It is, of course, possible to generate samples that will not satisfy equation (\ref{2}). I ask **is it non-expansive in expectation?**

My approach is:

$$\lvert\,\mathbb{E}_{a \sim Q}[f(a)] - \mathbb{E}_{a \sim Q}[g(a)]\, \rvert \leq \,\,\mathbb{E}_{a \sim Q}\lvert\,\,[f(a) - g(a)]\,\,\rvert \tag{4} \label{4} $$

Equivalently:

$$\lvert\,\mathbb{E}_{a \sim Q}[f(a) - g(a)] \, \rvert \leq \,\,\mathbb{E}_{a \sim Q}\lvert\,\,[f(a) - g(a)]\,\,\rvert$$

(\ref{4}) is true since:

$$\lvert\,\mathbb{E}[X] \, \rvert \leq \,\,\mathbb{E} \,\,\lvert\,\,[X]\,\,\rvert $$

**But, I am not sure if proving (\ref{4}) proves the theorem. Do you think that this is a legit proof that (\ref{3}) is a contraction.**

(If so; this would mean that stochastic policy q learning theoretically converges and we can have stochastic policies with regular q learning; and this is why I am interested.)

Both intuitive answers and mathematical proofs are welcome.

Your question in not very clear to me. Since $f(a)$ and $g(a)$ are not clear to me. The formulas are intuitive and individually correct, I am not sure whether arriving at those intuitive forms you have mentioned, so easy. Check this link for example: users.isr.ist.utl.pt/~mtjspaan/readingGroup/ProofQlearning.pdf  As a side not I do not think proving convergence is so easy. There is a topic called Concentration Inequalities which have to be studied to prove convergence. I think you can use this to prove your theorems.

(1) is a bellman update; it is a copy paste error that rhs has t+1 (sorry about that) thanks for noticing; I fixed the error now.


Dynamic Bertrand competition when players take turns

Consider the following game:

  * There are two players, $i\in\\{1,2\\}$
  * Time is discrete and runs to infinity during periods $t=\\{1,2,\ldots\\}$
  * At eat point in time, players have a price $p_i(t)\in\mathbb{R}_+$
  * Initialise the game with $p_1=p_2=p(0)$.
  * In odd-numbered periods, player 1 can change his price to any $p_1\in\mathbb{R}_+$. Player 2 cannot change his price.
  * In even-numbered periods, player 2 can change his price to any $p_2\in\mathbb{R}_+$. Player 1 cannot change his price.
  * For each price, there is a demand $D(p)$, which is a decreasing function. The firm with the lowest price at the end of each period captures the whole demand, receiving payoff $D(p_i)p_i$ for that period. The firm with the highest price receives a payoff of zero for the period. If prices are equal then each firm gets a payoff of $pD(p)/2$.
  * Players discount the future at common rate $0\leq\delta\leq1$ so a payoff of $\pi$ that occurs $t$ periods in the future has present value $\pi\delta^t$.
  * Write $p^*$ for the monopoly price that maximises $D(p)p$.

The question is: can we identify a complete characterization of the set of Nash equilibria of this game?

Note that a valid strategy is a complete contingent plan, which specifies the choice of action for any history of the game.

This question follows the discussion here: [What determines the outcome of a price war, and why isn't that outcome reached instantaneously?](https://economics.stackexchange.com/questions/8398/what-determines-the-outcome-of-a-price-war-and-why-isnt-that-outcome-reached-i?noredirect=1#comment10737_8398)

Is the Maskin-Tirole ECTA 1988 (the second paper, oligopoly) what you are looking for?

@denesp You're right. The more I think about it, the more the premise of the question doesn't make much sense.

Yes but the grim strategies are just a subset of all possible strategies. And what you describe is actually just a subset of all grim strategies, the condition to go grim can be quite ridiculous: "Play $p$ unless the other players last six prices were not $\pi, \pi, 2, 2, \sqrt{2}, \sqrt{2}$ in which case play 0." This strategy can be part of an equilibrium (given some restrictions on $\delta$). So a characterization of the kind you describe would not be a characterization of all equilibria.

@densep By characterization I mean a description of every Nash equilibrium. As you note, there are infinitely many equilibria just considering grim trigger strategies, so it is impossible to individually enumerate them all. But those equilibria can be concisely 'characterised' as "play $p$ unless someone ever played a $p'\neq p$ in which case play 0", which should work for $\delta$ large enough.

The word characterization is problematic. Saying that the strategies you are looking for are the strategies that constitute a Nash-equilibrium would be a tautology, but it is a characterization nonetheless. Such a tautology can be made less trivial by giving an alternate definition of Nash-equilibrium. I am not sure this is what you are looking for but you could say something about the range of payoffs: It seems to me that literally any payoff vector between the competitive solution and the cooperative solution is attainable in equilibrium using grim strategies.


a moving average relation that I can't find a counter-example to

Hi: I'm new to this list but I had a question that's not exactly related to dsp but maybe in a slight way and I didn't know where else to send it. So, here goes and my apologies if this is totally the wrong place to send it.

The following describes the background as far as number generation.

Suppose have a series of positive numbers coming in sequentially ( regularly spaced ) that are generated by some invisible machine or oracle. no distribution is assumed for these numbers ( except that they are positive ) and they are generated at t1, ... tn. they are labelled as $x_1, x_2, ... x_n, x_{n+1}$. at each time $t$, the moving average with window size $n$ of the log of the $x_i$ is calculated and is labelled $mavelogx_t$.

the parameters of are $n$ and $d$. $n$ is the length of the moving average calculated at each time $t$. $d$ is positive and will be explained below.

The following describes the game for a player who decides to play given the above ( assuming that atleast n numbers have been generated by now so a moving average can be calculated ). It doesn't cost anything to play this game.

The player of the game sees a new number at time $tstar$, $x_{tstar}$, and if $log(x_{tstar})$ is $d$ less than the current moving average, $mavelogx_{tstar}$, then $x_{tstar}$ is marked as the player's "initial" number. like 36.83 or whatever. the game works so the next time $log(x_t)$ crosses the moving average, $mavelogx_{t}$, from below at say $t = tstarstar$, then $x_{tstarstar}$ is marked as the player's ending number and the game ends.

The game rule is such that at $t = tstarstar$, the game ends and the player receives $winnum$ = $log(tstarstar) - log(tstar)$ ( winnum could be negative in which the player of course pays ). Of course it could happen that the player never get an initial number or that the player gets an initial number and never gets an ending number but let's make the assumption that the player plays and the game both starts and ends.

Now, I claim ( and think I proved ) the following two things. ( really one because it's an if and only if ) are true for the player playing the game.

A) if $(tstarstar - tstar)$ turns out to be less than $n$, then $winnum$ has to be positive. similarly if $(tstarstar - tstar)$ turns out to be greater than or equal to $n$, then $winnum$ has to be non-positive.

and conversely

B) if $winnum$ is positive, then $(tstarstar - tstar)$ has to be less than $n$. similarly, if $winnum$ is not positive , then $(tstarstar - tstar)$ has to be greater than or equal to $n$.

I could send the proof to anyone who is interested but my questions were :

A) does anyone have a counter-example to disprove what I'm saying in A) or B).

B) if A) and B) are true ( my proof is correct ), then is this a well known result in the literature ( any literature. doesn't have to be DSP ) ?

thanks a lot. Mark

also, a different explanation of the same problem is in the article at arxiv.org/abs/1212.4890. it's very long but might be clearer. thanks again.

sorry jason. my mistake. it should read that $winnum = log(x_{tstarstar})−log(x_{tstar})$. thank you for pointing that out.

The above description of your game is very difficult to understand. I would try to clarify it with a more succinct explanation. For instance, I don't see how $t^{**}$ could ever be less than $t^*$ according to your description, so $log(t^{**}) - log(t^*)$ would always be nonnegative.


Contribution in action from collisions for a gas?

So if I write the [action](https://en.wikipedia.org/wiki/Action_\(physics\)) for a gas:

$$ S = \int (T -U) dt \tag{1}$$ where $T$ is the kinetic energy and $U$ is potential energy.

I suspect there is a constant term (which does not affect the equations of motion) for the gas (in classical mechanics). I was hoping to verify my ~~[paper's](https://drive.google.com/file/d/1deuIYhorbMBnIRoblhe_0q1P4CkIx3DD/view?usp=sharing) idea ~~ (There is now an updated version see [here](https://drive.google.com/file/d/1K95v8crlBWsf2_3kFiEbRDMNa5xVBdOV/view?usp=sharing)) up to equation $(19)$.The summary of ideas section wise are as follows:

$(1.1)$ We consider an arbitrary potential which $0$ everywhere until both the gas molecules collide.

$(1.2)$ Assuming a non-ideal collision (finite collision time) I get a non-zero action

$(1.3)$ We use Newton's second law to find the finite collision time and the average force is considered as pressure.

$(1.4)$ The deformed face of the molecule has an area which is also considered.

$(1.5)$ Since we are considering $2$ spheres colliding which is a function of their relative velocity we can find the average momentum change assuming the collision is not a special event.

$(1.6)$ The collision per unit 4-volume is mentioned as derived by Einstein.

Combining all the above subsections we derive a constant action term:

$$S_c = \int \frac{N R \tilde V'(2R)}{\pi} \frac{ \langle \mu \rangle \lambda }{k_B } dt \tag{2}$$

where $N$ is the number of particles, $R$ is the radius of the particle, $ \tilde V'(x)$ is the spatial derivative potential and $ \frac{ \langle \mu \rangle \lambda }{k_B } $ are constants.

Is this expression correct?

(by linking to your paper you are now Less Anonymous :)


REALLY important days in history

@Z. Dailey asks a mathematical puzzle [Important days in history... and to come!](https://puzzling.stackexchange.com/questions/29503/important-days-in-history-and-to-come) with a series of dates in years 1973, 1978, 1983, 1992, 2005.

My puzzle asks the question: What is the previous date in my series? Explain!!

    .......... .., .....
          June 22, 1973
    .......... .., .....
          June 16, 1978
          June 11, 1983
         March 25, 1992
         April 14, 2005

Hint:

> "rudder as a male swan"

I'm assuming it's not January 3, 1972 and April 1, 1975...

@Haobin Is the hint in any way related to the Lufthansa logo?

Nothing failed about it. Also it's valid from a math p.o.v. ;check the MM/DD also.

@Overmind I would question your failed conclusion.

Logic dictates there's a high chance that the year is 1969, the fake moon landing year.


Can you find the cipher that I used?

I have encoded a phrase, using a cipher that I invented. But I don't want you to decode it, rather I want you to figure out how my cipher works.

This is the phrase that I encrypted

> Find the connection between this phrase and the output. You don't even have to do any solving, just figure out how this was encrypted

This is the key that I used

> A Blemished Orange

This is the encrypted phrase

> 1YaF7\B*,IdM#mI7\$iEB8lbnOKl5o.f9,J+772V2??16;XV2[)nhOov4elOT@h?Iw7nAO-2Nt2*,Sdq7XW&lBW0%^>u-C>,0)XSV&(627K,lHw7XeL8\B$PSj@[7n?PBbYbm@

These are important, but you'll have to figure out how

> 20
>
> 9100, 9129
>
> Python

That's everything, see what you can find!


A Glutton and a Split Check

**N** , **T** , **A** , **B** are arbitrary constants in this problem. I have only definitively solved the **N=1** subproblem, so, if the general problem cannot be solved, I will be satisfied to mark as accepted a solution to this subproblem. I may put a bounty out for the general solution before that.

## Problem Statement

You and **N** friends are seated at a restaurant, where you have agreed to equally split the check for \$**T**. The dining process consists of a waiter going around the table, asking each guest in turn to order an entrée costing any amount of \$1, \$2, ..., up to \$**A** (you cannot skip an entrée). The meal is of course over once the \$**T** limit has been reached (it cannot be exceeded).

The vain glutton that you are, you want to:

  1. eat more than you pay for (that is, \$**T/(N+1)**), but also;
  2. not be exposed as a glutton, which in this case consists of eating more than \$**B** extra than the next most expensive eater. If (1) cannot be achieved, you will nonetheless consider it a victory to see someone _else_ exposed in such a fashion.

Assuming you are first to order, for what values of **N** , **T** , **A** , **B** can you guarantee victory, no matter how your friends order? Keep in mind, they may conspire to make it difficult for you...

## Example

If **(N, T, A, B) = (1, 21, 13, 10)** , your gluttony might tempt you to order the most expensive \$13 entrée right off the bat. This would be a mistake, because your friend could thereafter order only \$1 entrées and expose you as a glutton. Similarly, you shouldn't order an \$11 or \$12 entrée. Yet if you order a $10 entrée or less, your friend may order an \$11 entrée, and you wind up paying for more than you eat while he cannot be exposed. So, there is no victory in this example configuration.

@Feryll Ah sorry, missed that N was your friends, not the number of people at the table. I see now the problem statement clarifies that. Thanks!

You have N friends, so, there are N+1 people in total who have precommitted to splitting the $T check. What they have not decided in advance is who is ordering what.

Hmm I just re-read the question and I'm a bit confused. How are they "equally splitting the check for \$T" if by the definition of the rest of the problem some people are ordering food that costs more than others' orders? Is everyone spending \$T/N regardless of how much they ordered for? If so, then not sure I understand \$T/(N+1) -- if everyone is spending \$T/N then aren't you "eating more than you pay for" starting from orders worth \$(T/N)+1 or more, rather than \$T/(N+1)? Are the parentheses applied incorrectly in the problem statement?

@Petr Naryshkin He will go around the table as many times as it takes to reach a total check of T, yes.

If the waiter gets all your orders and the check amount T is not reached (like in your example) does he start another round? If not, do you want to still eat for at least T/(n+1) or only for (total check)/(n+1)?

Reminds me of this a little bit en.wikipedia.org/wiki/Pirate_game

Funny, N>1 case seems easier than N=1, because the other's don't have to worry about being exposed (unless b=0). Still, too many cases for me  ;-)

Yes, it says so in the least paragraph of Problem Statement :)


Who wins the game of "Sticky Gomoku" on an infinite board?

This is a [Gomoku](https://en.wikipedia.org/wiki/Gomoku) like game, played by two players with Go pieces (black and white stones) on an infinite (in all four directions) Go board.

Rules

  1. **Four in a row:** players alternate turns placing a stone of their color on an empty intersection. Black plays first. The winner is the first player to form an unbroken chain of four stones horizontally, vertically, or diagonally.
  2. **Sticky:** except for black's first stone, every stone must be placed adjacent to one of your opponent's.

Question: can black force a win?

@Eric I see, thanks for the clarification! I guess it would be very hard to solve, as one doesn't expect to find a finite game tree that totally describes optimal moves.

@WhatsUp I heard of this game from an internet friend, who allegedly had invented it a few years ago, and had played it with friends since. The conjecture is that optimal play for both players will end in a draw, but no one is sure.

Looks like a funny game to play with family or friends, but I suspect that a systematic study would simply be an exhaustive search, probably computer-aided. Maybe @Eric can clarify whether there is a clever approach to be expected.

The only possible endgame I think is to create two 3-in-a-rows with one move, where the move itself and the two fourths are pairwise adjacent (forming a small triangle). Obviously the problem is how to force such a position in the first place...

This is actually much tricker than I thought. Getting 3-in-a-row is rarely an immediate threat because the blank space is usually not adjacent to the opponent and so not immediately playable.

@BenjaminWang Sure. Every intersection has eight adjacent neighbors.

Does "adjacent" include diagonally adjacent?


Best moves for a 7X7 4 queen board

There is a 7x7 board and 2 players, each having 2 queens. The queens can move just like the queens in chess, but cannot capture each other. At the beginning, the first player places both the queens on any 2 different squares. Then the second player does the same with his queens. From this point, the players alternate turns, moving both queens each turn. When a queen is moved, the square that was previously occupied is now blocked and cannot be stood on or moved across for the remainder of the game. The first player who is unable to move any of his queens loses!

I am designing a player for it. What will be the strategy for the player who starts first or second? Also, what are best starting moves for any player?

@SuhailGupta I hope that this  answers most of your doubts.      [1]:

didn't read the "can't move across" part sry, so I guess they can't.

If I have two queen in A1 and B1 can I make the queen in A1 move into C1? At the same time I choose to move my queen in B1 in C3! In other word are the moves of the two queen (of the same color) simultaneous or sequential.

I made a suggested edit to clarify the rules. If I got anything wrong please edit or roll back.

@Bass No. As the question says the end will be: "The first player who is unable to move any of his queens loses!"

Since "the queens can move just like the queens in chess", can they also capture the opposing queens?

It's still unclear for me if a player should move both his queens on his turn, or only one of them. Please clarify!

@prog_SAHIL The first move could be (3,4) and (5,5) for player 1 and then move for player 2 could be (1,2) and (2,2)

Can you please give an example for first 3,4 moves. Its hard to understand the rules.

After the first player has placed his two queens. They alternate their turns

When will the second player place the queens?


What is a 255b Word™?

This is in the spirit of the [What is a Word/Phrase™](https://puzzling.stackexchange.com/search?q=%22what+is+a%22+word+is%3Aquestion) series started by [JLee](https://puzzling.stackexchange.com/users/463) with a special brand of [Phrase™](https://puzzling.stackexchange.com/search?q=%22Phrase%22%20user%3A463%20is%3Aquestion) and [Word™](https://puzzling.stackexchange.com/search?q=%22What+is+%22+%22Word%22+-missing+user%3A463+is%3Aquestion) puzzles.

* * *

If a word conforms to a special rule, I call it a **255b Word™**.
Use the following examples below to find the rule.

$$ % set Title text. (spaces around the text ARE important; do not remove.) % increase Pad value only if your entries are longer than the title bar. % \def\Pad{\P{2.5}} \def\Title{\textbf{ 255b }} % \def\S#1#2{\Space{#1}{20px}{#2px}}\def\P#1{\V{#1em}}\def\V#1{\S{#1}{9}} \def\T{\Title\textbf{Words}^{\;\\!™}\Pad}\def\NT{\Pad\textbf{Not}\T\ }\displaystyle \smash{\lower{29px}\bbox[grey]{\phantom{\rlap{rubio.2019.05.15}\S{6px}{0} \begin{array}{cc}\Pad\T&\NT\\\\\end{array}}}}\atop\def\V#1{\S{#1}{5}} \begin{array}{|c|c|}\hline\Pad\T&\NT\\\\\hline % \text{ AMBIGUOUSLY }&\text{ AMBITIOUSLY }\\\ \hline \text{ BOXING }&\text{ BOXER }\\\ \hline \text{ COMPLICATE }&\text{ COMPLEX }\\\ \hline \text{ DECENTRALIZE }&\text{ DECENTRALIZATION }\\\ \hline \text{ ENTERTAINMENT }&\text{ EXCITEMENT }\\\ \hline \text{ FINALLY }&\text{ FINISHED }\\\ \hline \text{ GEOGRAPHY }&\text{ GEOLOGY }\\\ \hline \text{ HENRY }&\text{ HIGUAIN }\\\ \hline \text{ INDICATION }&\text{ INCLINATION }\\\ \hline \text{ JUXTAPOSING }&\text{ JUXTAPOSITION }\\\ \hline \text{ KNOWLEDGE }&\text{ KNOWLEDGEABLE }\\\ \hline \text{ LIGHTNING }&\text{ LIGHTENING }\\\ \hline \end{array}$$

And, if you want to analyze, here is a CSV version:

    255b Words™,Not 255b Words™
    AMBIGUOUSLY,AMBITIOUSLY
    BOXING,BOXER
    COMPLICATE,COMPlEX
    DECENTRALIZE,DECENTHALIZATION
    ENTERTAINMENT,EXCITEMENT
    FINALLY,FINISHEO
    GEOGRAPHY,GEOLORY
    HENRY,HIGUAIN
    INDICATION,INCLINATION
    JUXTAPOSING,JUXTAPOSITION
    KNOWLEDGE,KNOWLEDgEABLE
    LIGHTNING,LIGHTENING

The puzzle satisfies the series' inbuilt assumption, that each word can be tested for whether it is a 255b Word™ without relying on the other words. These are not the only examples of 255b Words™; many more exist.

**Problem: What is a 255b Word™? And why is the property called 255b?**

* * *

**Hint:**

> This rule is actually quite arbitrary. I don't think you can read my mind without the [steganography](/questions/tagged/steganography "show questions tagged 'steganography'") tag.

@Vepir Yeah, I deliberately misspelled a few letters in CSV. :)

Is the CSV misspelled? ("DECENT'H'ALIZATION" with 'H' in CSV, but with 'R'  in the table) Is CSV purposely not fully capitalized? (such as 'g' in "KNOWLED'g'EABLE" and 'l' in "COMP'l'EX")


Need help with a cipher: "poems" with capital letters

I am trying to figure out a cipher I _think_ I found in a book from 1982 and it's a 36 year old mystery we are trying to solve (I will reveal the book if this turns out to be something so everyone can play along!). The book has several poems in it that has has lines that start with capital letters. There is also several things that should be capitalized (like a name or place) that aren't capitalized and things that randomly made their own line in the puzzle. I thought it was weird so I typed them out and there were double and triple letters. I tried putting them in online puzzle solvers but nothing came up except gibberish. I also thought maybe the spaces meant new words, but nothing!

There is another post that I saw (that's how I found this site) that was similar using a columnar transposition cipher but I'm not sure if it is the same!

Three of the twelve did not have any double letters and there are several others poems in the book that mean something.

> FCFTTNTNISSPISTWOFIDLTA
>  AFIFOAFHWTGFTN
>  ITNXTIAINWWFYFAAETLI
>  BAIBFBOSFSPAFTSF
>  LTYWTOWCABBOTWAFGT
>  OMCSBSHHFOMEEOWBBBEWFW
>  ATNHEJFSNRIIOTGGTT
>  VMAAFAFAFSSCAAPTBWSTPYOOAT
>  TWNWBAAYSSSELOY
>  IOFEIYCANSITOHVTOFOLAIOTB
>  PIDRTTDWWAIJAATUWOLATBDY
>  WMBACRLBIFFTTIFCFSOBH

These are the "extra" poems:

> TNAESFSSFWBDFAEM
>  WFFEFEGQBCCETHISTFFRDAIGTOLGAATRTPIATRASADBTNHAFCPCEGVEWTFF
>  KTTT
>  NWEGCTFFMTDSTFANATSSNH
>  TTSIBMHAFFWTWOAAFETFF
>  ASYFCTTWWFFFG

@Rubio As far as I can tell no one has come up with this cipher.  I believe the only ones that are important are the first letters (besides using the clues in the verse to guide you too) as part of a secret cipher for several reasons.Preiss doesn't give us a way in the book to match up the picture and the verse, only tells us to match them up.There are a ton of combinations that they could be so there has to be a way for us to tell which is which.If you google the verses you will see that things like oz and wonderstone aren't capitalize, but they should be because they are places/people.

Yes, its real and not gibberish.  I will just tell you guys what the book is because I know I can't figure it out.  Hopefully someone can and find one of the treasures.  In 1982 a guy named Byron Preiss wrote a book called The Secret, a Treasure Hunt and hid 12 casques throughout 12 North American cities.  Since 19821, only 2 of the 12 have ever been found (Chicago in 1983 and Cleveland in 2004).  The poems are long (all of them put together), otherwise I would post them.  If you google the book you can find all the poems and what cities people think they are in.  Good luck!

Can you post the full poem as well? How can we be sure that this isn't just gibberish you pulled?

How sure are you that the first letters are the only important parts here?


Rare but Abundant in Hill and Hole

> I am uncommon,
>  But a worthy goal.
>  Rare but abundant
>  In hill and hole.
>  Who am I?

You may already know the answer, so the task is not to answer the riddle per se, but to solve the following 'What is a Word' puzzle, use the words in the puzzle to find another four word phrase (2 from each side), and explain how the phrase provides/validates the answer to the above riddle.

> What is an Uncommon Word?
>
> If a word conforms to a special rule, I call it an Uncommon Word.
>  Use the examples below to find the rule.

Uncommon Words | Not Uncommon Words
---|---
FEW | LOTS
MANY | ALL
RARE | ATYPICAL
TIE | UNHINGE
IA | RI
BAGGED | TOSSED
DOOMED | PERISHED
GAPING | BEHOLDING
ADO (2x) | TWICE
SEAL | REFUSE
HAVE | LACKED
ROUNDS | TURNS
SPITS | SPITES

> These are not the only examples of Uncommon Words, many more exist.

Hint:

> Teepee is an extremely common word.
>  Quitting is an extremely uncommon word.
>  Puddle is a quite uncommon word and also quite relevant. What property makes something uncommon?

Hint 2:

> I updated a word so that all the uncommon words are in an American scrabble dictionary rather than simply being American-English words, to help solvers identify them. Also added a very useful tag.


Can a calculus have incremental copying and closed scopes?

A few days ago, I proposed the [Abstract Calculus](https://medium.com/@maiavictor/the-abstract-calculus-fe8c46bcf39c), a minimal untyped language that is very similar to the Lambda Calculus, except for the main difference that substitutions are `O(1)` (i.e., variables only occur once) and copying is an explicit, constant-time and incremental operation. Here is an example:

    let (a & b) = (λx. λy. λz. y) in (a & b)
    ---------------------------------------- step 0
    let (a & b) = (λy. λz. y) in (λx0. a) & (λx1. b)
    ------------------------------------------------ step 1
    let (a & b) = (λz. y0 & y1) in (λx0. λy0. a) & (λx1. λy1. b)
    ------------------------------------------------------------ step 2
    let (a & b) = y0 & y1 in (λx0. λy0. λz0. a) & (λx1. λy1. λz1. b)
    ---------------------------------------------------------------- step 3
    (λx0. λy0. λz0. y0) & (λx1. λy1. λz1. y1)

Here, the `let (a & b) = t in k` syntax substitutes the occurrences of `a` and `b` in `k` by `t`, except it does so incrementally: notice how each lambda is pulled one by one.

There is a problem, though. Eventually, you'll try to pull a lambda which has a variable that is bound in `t`. That's occurs with `y` behind step 1. To solve that issue, whenever that happens, the bound variable `y` is replaced by `y0 & y1`, which are bound to each of the two copied lambdas, respectively. The problem with that is this requires us to abandon the notion of scopes, as, now, the bound variable of a λ can occur outside its own body (such as `y0` and `y1` behind step 2)!

Despite sounding bizarre, the language operates very well. You can represent the usual λ-encodings on it, implement recursive algorithms and it is compatible with the oracle-free fragment of the optimal reduction algorithm. Yet, some have questioned the absence of scopes. I've spent some time reasoning about this and couldn't find a way to have both things. In fact, it looks impossible. As a sanity check, I'd like to know if my reasoning makes sense.

**Thus, my question is: is that possible? Can we have both incremental copying and closed scopes in a calculus?**

@chi I was reading Lamping's original paper right now, seems like he doesn't propose a textual calculus indeed. What I proposed is just a textual interpretation of his algorithm (in any of its stages). That representation can itself can be considered a calculus with 4 reduction rules, but requires abandoning the notion of scopes, which is what is puzzling me. Thanks (:

I'm not terribly familiar with graph-reduction techniques for the lambda calculi. I only remember that some later variants were much simpler than Lamping's (fewer brackets/special tokens around). Perhaps there's some way to represent those "bracket" rules in a term form? I vaguely recall a presentation stating that those rules could not be used on terms -- they really need graphs --, but I am unsure.

@augustss you're right, I should've clarified that I meant a language with abstractions λx. f.

@chi yes the reason I came up with this was to have an untyped (textual) language for Lamping's abstract algorithm. This is just a textual interpretation of that algorithm. Or do you mean he actually proposed such representation? I'm not aware of that, but would be very glad if that was the case. I'll check the paper again.

To me, this looks similar to the graph-reduction algorithms for (optimal) reduction in lambda calculus. The first paper IIRC was at POPL'90 by Lamping. There, the strict notion of scope is "broken" as it happens above, and replaced with a plethora of (indexed) bracket-markers in the term graph, which is reduced using small steps as above, duplicating terms gradually. I'd recommend you have a look at it, and subsequent (simpler) graph-reduction techniques.

SKI combinators have incremental copying, are (almost) isomorphic to the lambda calculus.


Is the two-color leapfrog problem in P?

My question is whether a specific decision problem is in P or not. It's straightforwardly in NP. The decision problem is a specific case of the general $k$-color leapfrog problem.

I can already show that when $k=1$, the problem is in P. And when $k$ is unlimited, the problem is NP-complete. I have not been able to find an algorithm for the $k=2$ case, but I suspect it is in P. I am wondering if we can prove that it is in P, or (what would surprise me) that it is in fact NP-complete.

This question springs from an interesting discussion in a question I [previously asked](https://cs.stackexchange.com/questions/127268/is-the-leapfrog-automata-problem-in-p) on this site.

> **The $k$-color leapfrog problem**. Fix $k$. In a $k$-color leapfrog problem, you are given some number of transparent urns containing colored balls. Each ball is one of $k$ possible colors. You are also supplied with an _agenda_ , which is an ordered list of colors, for example "red, red, red, blue, red, green, ...". The object of the game is to draw balls out of the urns, one at a time, following the sequence of colors specified in the agenda. You can easily see the contents of all urns at all times, and can draw any ball out at will. If you could draw the appropriate color from multiple urns, you may choose which one. The only constraint is that you may not draw from the same urn twice in a row. You win the game if you complete the agenda, otherwise you lose.
>
> The decision problem is whether, for a given arrangement of urns and a given agenda, the game is winnable.

**The 1-color leapfrog problem is in P**. If there is only one color of ball, you can decide it with the following algorithm: at each turn, draw the ball from the urn with the most balls in it (among urns you're allowed to draw from). If the game is winnable at all, this algorithm will win it. (Note that even when $k=1$, a game may still be unwinnable if there's too great a disparity in ball count. For example, the setup ["R", "RRR"] with the agenda "RRRR" is unwinnable.)

**The $\infty$-color leapfrog problem is NP-complete**. See my [previous question](https://cs.stackexchange.com/questions/127268/is-the-leapfrog-automata-problem-in-p) and the dazzling solution provided. You can perform a reduction from SAT using a game with two new urns and three new colors per clause.

**What about 2-color leapfrog?** This is my main question. I suspect the problem is in P, as with the 1-color case, but the 1-color algorithm doesn't seem to generalize — or at least I'm not sure.

**Bonus: What about $k$-color leapfrog?** An interesting higher level question is where exactly the problem becomes hard. Is is still in P when $k=3$, or does the leapfrog game have a phase transition like 2SAT to 3SAT? Is perhaps in P for all finite $k$? I've been struggling to find an efficient algorithm— I suspect there might be one, just based on how the problem becomes smaller and smaller as you remove balls.

* * *

**Edit** : _The naive extension does not work._ Note that the "leveling" strategy — generally ensuring that the urns have approximately the same number of balls of each color by removing from the urn that has the most— perfectly solves the $k=1$ case. I expect finding an extension of this strategy will solve the $k=2$ case, but note that one naive extension does not work:

For the scenario where the urns contain [AAB, ABB] and the agenda requires AABBBA, the naive strategy of taking from the legal urn with the most of the requested color does not work. It fails to find a solution despite the fact that this instance is actually solvable. (Similarly, the strategy of taking each color from the urn that has the least amount of the _other_ color also fails on this problem instance.)

* * *

**Update:** In the special case that the agenda has the form $A^m B^n$ $(m,n\geq 1)$, you can determine whether it's solvable in polynomial time by playing two single-color games in series. Briefly put, a single-color game is winnable if and only if the vase with the most balls has at most one more ball than the rest of the vases put together. Equivalently, it's winnable if and only if you can pair up each ball with a ball from another vase, with at most one left over. You can construct a winning sequence of moves given such a pairing: first draw the unpaired ball if any. Then iterate over each pair in an arbitrary order. Because the pairs come from different vases, at least one of them will be legal to draw next; draw it, then the other one. Proceed until you draw all the balls.

Because you can iterate over the pairs in an arbitrary order, you can generally always choose a drawing order such that the last ball comes from the vase of your choice. Or, reversing the drawing order*, that the first ball comes from the vase of your choice. Hence in the two-color scenario, when the agenda is $A^mB^n$, you can solve it as follows: Pick a vase $V_A$ with an $A$-colored ball and another vase $V_B$ with a $B$-colored ball (if you can't, then there's only one vase and it has multiple balls in it, so the game is unwinnable). If it's possible to win the $A^m$, choose the winning strategy to draw from $V_A$ in the last step. If it's possible to win the $B^n$ game, choose the winning strategy to draw from $V_B$ in the first step. This can all be done and checked in polynomial time, QED.

[*] Actually, reversing a path is not always possible: if the vase with the largest amount has exactly one more ball than the rest of them put together, then you _must_ draw from the largest vase first. The proof for how to solve $A^mB^n$ in this case will have to wait.

* * *

**Update** : By exchanging the notion of urns and colors, you can get the following equivalent formulation:

> _Reformulated leapfrog problem:_ Fix $k$. You are given $k$ urns full of different-colored balls, and an agenda which tells you which urn you must draw from at each step. Your goal is to empty all of the urns subject to the constraint that each ball must be a different color from the one before it.

Perhaps rephrasing this way can be useful. Also in this reformulation, I think the complexity is exposed by supposing you have two urns, where the second urn contains at most two balls.

A case is a set of additional constraints that we might choose to impose. E.g., "Is there a solution in which urns 3 and 5 finish with at least 1 A-ball each, and urns 2 and 5 finish with at least 1 B-ball each?" The idea being to come up with a set of at most polynomially many cases such that (a) any solution to the original problem belongs to at least 1 case in the set, and (b) each of these more constrained problem instances can be solved in poly time -- solving each case would then give a poly-time algorithm. But now I think (b) is harder to achieve than I thought.

@j_random_hacker If we restrict either the maximum number of urns or the number of balls per urn, the search problem becomes polynomial, but I'm not sure yet that it's polynomial without those restrictions.  In the first case, a game state lists the number of balls of each color in each urn. In the second, the contents of each urn have a constant number of possibilities, so a game state counts how many urns of each content-type there are. In both cases, the number of game states is polynomial in the input.

Thanks.  I'm wondering what counts as a "case" --- is it a game state (i.e. the contents of each urn at a single point in time) or a game path (i.e. a sequence of draws to make)?  Naively, there are a lot of paths --- each game state will generally have multiple ways to arrive at it, and some might not be legally reachable.

The above is not quite right: Once there is only one A left in the agenda, if there is only one urn with A-balls in it and it is also one of the 2 urns with B-balls, then we cannot necessarily choose B-ball urns freely. Still I think this problem can be attacked with a case analysis: Either there is a solution where we never drop below 2 A-ball urns and 2 B-ball urns; or a solution in which some urn becomes the only A-ball urn "first"; or a solution in which some urn becomes the only B-ball urn "first"; or no solution. There are only $O(n^2)$ such cases to consider.

If there are not enough As and Bs in total to cover the letters in the agenda, then clearly the instance is unsolvable. Otherwise, the only way to get stuck is to get into a situation where two adjacent agenda letters can only come from the same urn. So if you can choose urns in sequence so that, while $\ge 2$ A-balls remain in the agenda, you have at least 2 distinct urns $a_1, a_2$ containing 1 or more A-balls, and similarly urns $b_1, b_2$ for B-balls (note $\{a_1, a_2\} \cap \{b_1, b_2\}$ is allowed to be nonempty), you will get a solution. However this can miss some solvable instances.

As a follow up, note that when the agenda is a simple band of colors, e.g. AAAAAA or AAAAABBBBB, the problem is quite simple because it factors mostly like the $k=1$ case; the hard part is deciding what to do at the junctions between neighboring colors.  If we can find a simple algorithm for the junctions, we can handle each color more or less separately and stitch them together in the end.

Assuming there is a polynomial algorithm, perhaps there's one that runs in time $O(n^k)$, where $k$ is the number of colors and $n$ is the problem size. This suggests an approach to designing an algorithm---iterate over each color, or even each pair of colors. And it would suggest why the problem becomes harder when the number of colors is unbounded so $k \approx n$.

It seems I wasn't careful enough, so there's no issue with encoding (we can assume that each vase has at most the agenda's length balls of each color, and since the agenda is ordered this means we are allowed a polynomial number of operations in the numerical value of the number of balls in each vase).

@Ariel Can you, please, elaborate on the $O(n^3)$ algorithm from your previous comment? If I understand you right, you will have to fill a $(n + 1)$-dimensional table with $n$ elements in each dimension at the worst case. But it will give an $\Omega(n^{n+1})$ time complexity.

My mistake, I missed the "draw any ball at will". The encoding becomes important here, each vase should be described as a histogram (n-white, m-black) otherwise the previous solution is polynomial.

@Ariel I should clarify that there's no order to the balls in each vase---you can see all of them, and pick any one out at will.  The agenda is a separate structure, listing the sequence of colors you must draw (and you can pick which vases). I like your approach, though, and a similar algorithm definitely works for $k=1$ colors. Maybe we can make it work for $k=2$?

Have you tried dynamic programming? Given $n$ vases each described as a list of colors, our ability to win the game depends on $n+1$ parameters, the index of the active vase (constraint) and $n$ indices of locations in each list which indicate the number of balls we have drawn from each vase. If I'm not missing anything basic, this seems to admit to a straightforward $O(n^3)$ time algorithm (assume for simplicitly that $n$ is a bound on both the number of vases and the number of balls in each vase). It also seems to work for the $\infty$-color problem, so I might be completely off.

@Juho Thanks for the tip! I'll look into color graph path problems; the leapfrog problem is definitely a special case of that. I suspect it's an easier special case, but if I can perform a reduction from GRAPH MOTIF,  that'll establish that it's in fact just as difficult.

It sounds similar to a problem in which you want to find a path in a (directed) graph with specified colors (in order). These type of problems are usually hard (like GRAPH MOTIF, which IIRC is hard for 2 colors even on trees). Your visitation constraint might complicate things a little (or maybe you can avoid it) but a reduction from such a problem might be short and simple.


Test whether two languages are equal, when give in algebraic form

This sub-problem is motivated by [Algorithm to test whether a language is regular](https://cs.stackexchange.com/q/18010/755).

Suppose we have two languages $L_1,L_2$ that are expressed in "algebraic" form, as formalized below. I want to determine whether $L_1 = L_2$.

Is this decidable? If yes, can anyone suggest an algorithm for this? If not, can anyone suggest a semi-algorithm (one that returns "equal", "not equal", or "I'm not sure"; where we want it to return "not sure" as rarely as possible).

Motivation: A solution to this sub-problem would be helpful for solving [Algorithm to test whether a language is regular](https://cs.stackexchange.com/q/18010/755)

* * *

**Algebraic form.** Here's one possible definition of what I mean by a representation of a language in algebraic form. Such a language is given by

$$L = \\{E : S\\}$$

where $E$ is a word-expression and $S$ is a system of linear inequalities over the length-variables, with the following definitions:

  * Each of $x,y,z,\dots$ is a word-expression. (These represent variables that can take on any word in $\Sigma^*$.)

  * Each of $x^r,y^r,z^r,\dots$ is a word-expression. (Here $x^r$ represents the reverse of the string $x$.)

  * Each of $a,b,c,\dots$ is a word-expression. (Implicitly, $\Sigma=\\{a,b,c,\dots\\}$, so $a,b,c,\dots$ represent a single symbol in the underlying alphabet.)

  * Each of $a^\eta,b^\eta,c^\eta,\dots$ is a word-expression, if $\eta$ is a length-variable.

  * The concatenation of word-expressions is a word-expression.

  * Each of $m,n,p,q,\dots$ is a length-variable. (These represent variables that can take on any natural number.)

  * Each of $|x|,|y|,|z|,\dots$ is a length-variable. (These represent the length of a corresponding word.)

My goal is something rich enough to capture many of the examples we see in textbook exercises. (Feel free to suggest modifications to this formalization if it makes the equality-testing problem easier while still remaining expressive enough to capture many of the examples we see in textbooks.)

* * *

**Some easier cases.** If the original problem is too hard, here are some sub-cases that would still be interesting:

  * If $L_1,L_2$ are two languages specified in "algebraic" form as above, and $L_1\ne L_2$, let $d(L_1,L_2)$ denote the length of the shortest word that is an element of one of $L_1,L_2$ but not the other. Can we upper-bound $d(L_1,L_2)$ as a function of the length of the descriptions of $L_1,L_2$? Is it guaranteed to always be polynomial? at most singly-exponential?

Motivation: If we could prove it is always polynomial, this might help us exhibit a witness that $L_1 \ne L_2$.

  * If we omit the reversal operation (we do not allow $x^r,y^r,\dots$), does the problem become easier?

If it is limited to regular languages, then you can compare them. Regular languages can be converted to finite state machine. And if you minimise two different finite state machines and you end up with the same answer for both, then they are infact the same. (They both handle the same language if they simplify to the same form.) You won't be able to do this for a CFG language, otherwise you would have solved the halting problem.

I don't see how to express Dyck languages in this framework.

@FrançoisG., no.  For instance, $\{w^\eta w^\eta : w \in \Sigma^*, \eta \in \mathbb{N}\}$ is not context-free but has an algebraic form.  (I don't know whether every context-free language has an algebraic form expression; I don't see any reason to expect this to be true.)  I know that testing equality of two context-free languages is undecidable.  I don't know how hard the first sub-case (at the end of the question) is if $L_1,L_2$ are two context-free languages.

Are those algebraic form languages and context free languages the same ?


What can be proven regarding the differences in power between unary ECMAScript regex functions and primitive recursive functions?

In 2014, inspired by [Regex Golf](https://alf.nu/RegexGolf), I started exploring, along with a mathematician going by the name **teukon** , what could be done in the unary domain in ECMAScript regex that went significantly beyond matching primes and powers of 2 (both of which were puzzles in the Regex Golf site). This is a dialect of regex that includes negative and atomic positive lookahead, and backreferences that are erased when the loop they were defined in starts a new iteration. (They're of course not "regular expressions" in the CS sense).

A unary regex function takes a string of identical characters as input (`x` is used for clarity instead of `1`), whose length represents $n \in \mathbb{N_0}$. Its output may be either a non-match (which can represent "false"), or any $m \le n$ (which can represent "true", ignoring $m$'s value).

Due to the limitations on ECMAScript backrefs, only one variable* can be changed from iteration to iteration in a loop – the the $tail$ following the current cursor position, in such a way that it is decreased by at least $1$ on every iteration (by moving the cursor forward). All other variables (backrefs) can only capture a value once (per iteration if in a loop) which cannot be greater than the value of $tail$ at the time of its definition. An additional constraint is that when searching for a number that satisfies some condition, the current value being tested must be subtracted from $tail$ before its properties can be tested (this can be avoided by defining a new operation, non-atomic lookahead, which allows the regex engine to backtrack into the lookahead from outside it). And $n$ can only be operated on directly when the cursor is at the leftmost position, i.e. $tail=n$.

Because of this, the approaches available in more powerful regex flavors (e.g. Perl, PCRE, Java, .NET, Python, Ruby) won't work, for example `^(^x|xx\1)*$` can't be used to match squares.

I was suprised and fascinated to find that the power of unary ECMAScript regex goes rather deep indeed:

  * Match primes - [`^(?!(xx+)\1+$)xx`](https://codegolf.stackexchange.com/a/177196/17216)
  * Match powers of 2 - `^(?!(x(xx)+|)\1*$)` or `^(?!(x*)(\1\1)+$)` or `^((x+)(?=\2$))*x$`
  * Match powers of 10 - `^((x+)\2{8}(?=\2$))*x$`
  * Match prime powers - [`^(?!(((x+)x+)(?=\2+$)\2*\3)\1+$)x`](https://codegolf.stackexchange.com/a/219939/17216)
  * Match `N`th powers - [`^((?=(xx+?)\2+$)((?=\2+$)(?=(x+)(\4+$))\5){N})*x?$`](https://codegolf.stackexchange.com/a/21951/17216)
  * Match correct statements of multiplication - [`^x(x*)·?(x*)·?x\1=(?=(x(\1\2))\1(\3*)\4*$)\3*$\5`](https://github.com/Davidebyzero/RegexGolf/blob/master/regex%20for%20matching%20multiplication%20-%20simple%20method.txt) (takes its input as `A·B=C`; doesn't handle zero); this uses the Chinese remainder theorem. Although it's not purely unary (with delimiters `·` and `=`), its multiplication logic can be used within a purely unary regex.
    * Fully generalized, with handling of zero: [`^(?=(x?(x*))·(x?(x*))).*=(?=.*((?=\1*$)(?=\3*$)(?=\1\4+$)\3\2+$|$\1|$\3))\5$`](https://github.com/Davidebyzero/RegexGolf/blob/master/regex%20for%20matching%20multiplication%20-%20multiplication%20%2B%20comparison%20-%20fully%20generalized.txt)
  * Division and remainder - [`(x(x*)),(x*?)(?=\1*$)(x?(x*))(?=\4*$)((?=\2+$)\2\5*$|$\4)`](https://codegolf.stackexchange.com/a/222932/17216)
    * This turned out to be quite interesting. The algorithm can't be proved using the Chinese remainder theorem, but finally does have a proof (linked above) thanks to H.PWiz. There are shorter variants that can be used when certain inequalities regarding the dividend and divisor are guaranteed to be met.
  * Match squares - [`^(x(x*)|)(?=(\1*)\2+$)\1*$\3`](https://codegolf.stackexchange.com/a/223201/17216) (shorter than the above Nth powers regex thanks to generalized multiplication)
  * Match perfect powers ($a^b$ where $b>1$) - `^(x+)((\1(x+))(?=(\3*)\1*$)\3*(?=\4$\5))+x$|^x?$`
  * Match triangular numbers - [`^((((x*)x?)\3)x)?(?=\4(\1*)\2*$)\1*$\5`](https://codegolf.stackexchange.com/a/220988/17216)
  * Match Fibonacci numbers - [`^(?=(x*).*(?=x{4}(x{5}(\1{5}))(?=\2*$)\3+$)(|x{4})(?=xx(x*)\5)\4(x(x*))(?=(\6*)\7+$)(?=\6*$\8)\6*(?=x\1\7+$)(x*)(\9x?)|)(\9\10(\5\10)\12?|xx?x?|x{5}|x{8}|x{21})$`](https://codegolf.stackexchange.com/a/178956/17216)
  * Match factorial numbers - [`^(((x+)\3+)(?=\3\3\2$)(x(?=\3+(x*))(?=\5+$)(?=((x+)(?=\5\7*$)x)\6*$)\3+(?=\5\7$))*\3xx|x?)x$`](https://codegolf.stackexchange.com/a/178979/17216)
  * Match abundant numbers - [`^(?=((?=(xx+?)\2+$)(x+)\3*(?=\3$))+(?!\2+$)(?=(xx(x*?))\4*$)x((x(x*))(?=\7*$)\5\8+$))(?=(x(x*))(?=\9*$)\6\10+$)(?=(x*?)(?=(x(\6\7))+$)(x(x*))(?=\14*$)(?=\13+$)\15+(?=\11\11|(x))\13$)((?=(x*?(?=\9*$)(?=(x+)(?=\10+$)\19*$)(^\14{2}\16|)))(?=(xx+).*(?=\9$)(?=\21*(?!\19)\21$)(|(x+)\23*(?=\23$))(?!(xx+)(?!\21+$)\24+$)(?=(x*)(?=xx(x\25)*$)\21\25*$)(\21+$))(?=.*?(?!x\18)(?=(x(\27\25))+$)(x(x*))(?=\30*$)(?=\29+$)\29\31+$|)(?=\30(x*)(?=\9*$)(\21\10+$))\32)+\33$`](https://codegolf.stackexchange.com/a/178952/17216) (there is a shorter version, but I haven't proved it to be correct to infinity yet)
  * Return $\lfloor{\log_2 n}\rfloor$ \- [`(?!(x*)(\1\1)+$)(?=(x+)\3)(?=((?=(x+)(?=\3)\5(((x*)(?=\8$)x)+$))\6)*)\5x|\b$`](https://chat.stackexchange.com/transcript/message/48931239#48931239)
  * Return $\varphi(n)$ \- [`(?=((xx+)(?=\2+$)|x+)+)(?=((x*?)(?=\1*$)(?=(\4xx+?)(\5*(?!(xx+)\7+$)\5)?$)(?=((x*)(?=\5\9*$)x)(\8*)$)x*(?=(?=\5$)\1|\5\10)x)+)\10|x`](https://codegolf.stackexchange.com/a/180255/17216)
  * Return $\lfloor{n\sqrt{1/2}}\rfloor$ \- [`(?=(x(x*)).*(?=\1*$)\2+$)(?=(x\1)+(x?(x*)))(?=\4(x(x*?))\1+$)(?=.*(?=(?=\4*$)\4\5+$)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\9))(?=.*(?=(?=\4*$)(?=\6*$)(?=\4\7+$)\6\5+$|$\4)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\13))(?=.*(?=\12\12\9$)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\17))(?*.*?(?=((?=\3*(x?(x*)))\21(x(x*?))\1+$)))(?=.*(?=\23*$)(\23\24+$))(?=.*(?=(?=\21*$)\21\22+$)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\27))(?=.*(?=(?=\21*$)(?=\23*$)(?=\21\24+$)\23\22+$|$\21)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\31))(?=.*(?=\30\30\27$)(x*?)(?=\3*$)(x?(x*?))(\1+$|$\35))(?=.*(?=\26\26)(?=\3*(x*))(\1(x)|))(?=.*(?=\34\34\40)(?=\3*(x*))(\1(x)|))(?=(?=(.*)\13\13\17(?=\6*$)\6\7+$)\44(x+|(?=.*(?!\16)\41|(?!.*(?!\38)\8).*(?=\16$)\41$))(\25\31\31\35){2}\43$)\20|xx?\B|`](https://codegolf.stackexchange.com/a/198428/17216) \- uses `(?*`...`)`; without that, it'd be about twice as long or more. This can clearly be extended to any algebraic number $0<x<1$; $x=\sqrt{1/2}$ is probably the simplest irrational example.

*So in order to emulate more than one mutable variable in a loop, a tuple must be encoded into a single number. For example:

  * The encoding $(a,b,j)\to a+b^2+jC$, where $j$ is a decreasing iteration count, works as long as $a\le 2b$ and $(b+1)^2\le C$ always remain true and $(j_0+1)C-1\le n$. The values of $a$ and $b$ may increase or decrease from iteration to iteration.
  * With the encoding $(a,b,j)\to a+bC+jC^2$, where $a<C$ and $b<C$, the values of $a$ and $b$ may increase or decrease from iteration to iteration without any inequality being enforced between them.
  * The encoding $(a,b)\to n-a-b^2$ works as long as $a\le 2b$ and $a+b^2\le{n\over 2}$ remain true and $a$ and/or $b$ increase on every iteration.

I have [written a regex engine](https://github.com/Davidebyzero/RegexMathEngine) to facilitate exploring mathematical regexes. It's much faster than traditional regex engines when operating on unary numbers, not only because it can represent them as integers rather than strings, but also thanks to optimizing for common operations done on unary numbers. I also implemented non-atomic lookahead / molecular lookahead `(?*`...`)`, which was later also adopted by the regex engine PCRE2.

In 2019 I began to post mathematical ECMAScript regexes on [CGCC](https://codegolf.stackexchange.com/users/17216/deadcode), which got [Grimmy](https://codegolf.stackexchange.com/users/6484/grimmy) and [H.PWiz](https://codegolf.stackexchange.com/users/71256/h-pwiz) interested in the subject. Some nice advancements came from this, including huge golf optimizations, and at least one additional function being implemented that I previously wasn't sure was possible, $\varphi(n)$, and a rigorous proof of the division algorithm. But my core questions remain unanswered.

### Please note that although there are five questions below, I will upvote and accept any good answer to even just one of them.

My questions are:

  1. Can any transcendental number $0<x<1$ be "computed" by an ECMAScript regex in the same way as $\sqrt{1/2}$ is computed by the regex above, i.e. by returning $f(n)=\lfloor{nx}\rfloor$? Trivial examples like $1.101001000100001...^{-1}$ might be possible. What about $\pi^{-1}$ or $e^{-1}$, or numbers whose class is not known, such as $\gamma$ or $\delta$?
  2. How precisely can the power of unary ECMAScript regex be characterized? Can any primitive recursive functions in $\mathbb{N_0}$ where $f(n)\le n$ be proven to be impossible? It's obviously somewhere below primitive recursive in power, as its limitations surely make implementing some primitive recursive functions impossible. For example, I'm pretty sure [$\pi(n)$](https://en.wikipedia.org/wiki/Prime-counting_function) is impossible. (Given infinite scratch space to work in, instead of just $n$, it would be exactly primitive recursive in power – just search for an enormous number encoding all the relations to be tested, and consume them one at a time.)
  3. Are there any functions that can be implemented with molecular lookahead `(?*`...`)` but not without it? Grimmy came up with an interesting problem that comes close: Match $n$ iff there exist perfect powers $a\le n$ and $b\le n$ (using the definition of perfect powers above) such that $n-a = (n-b)^m$ where $m>1$. This gives the sequence $20, 28, 29, 35, 41, 52, 68, 72, 89, 90, 126, 130, 133, 174...$
     * In ECMAScript+`(?*)` this is just [`^(?*(x(x*)x+))(?!(\1|x\2)(?!(x+)((\4(x+))(?=(\6*)\4*$)\6*(?=\7$\8))+x$|x$)).*(?=\1$)\2((\2(x+))(?=(\10*)\2*$)\10*(?=\11$\12))+x$`](https://kingbird.myphotos.cc/b751bb38f1a1b12e90f23541f8f6ec33/regex%20for%20perfect%20power%20differences%20puzzle,%20with%20molecular%20lookahead.txt).
     * In ECMAScript it's still possible: [`^(?=(x*)\1(x*))((x+(x*))(x?))(?=\6*(x?)).*(?=\1$)(?=.*(?=\2\7)(xx)?((\7?)x*))(?=\9*(x?))(?!\10(\4|\5)\6(?!$\11|(\11(x*))(?:(?=(?=\13$\9|(?=(\13\13\9)+(x*))(?=.*(?=\15)\16(x*))\17\15*$)((x*)(x|(?=(x))))(?=\21((?=\18$)|(?=(\18\18\21))(?=\18\18\23*(x*))(?=.*(?=\23)\24(x*))(?=\25\23*$)))((?=.*(?=\21$)\11(x*)|(x))(?=(\27\28))\21\14\28((?=.*(?=\29$)\20$)\19$|(?=(\19\19\20)+(x*))(?=.*(?=\31)\29\32(x*))\33\31*$)))(?=.*(?=\21$)\11$)\26\14)+$)).*(?=\6\4$)(\6(\5))(?:(?=(?=\34$\7|(?=(\34\34\7)+(x*))(?=.*(?=\36)\37(x*))\38\36*$)((x*)(x|(?=(x))))(?=\42((?=\39$)|(?=(\39\39\42))(?=\39\39\44*(x*))(?=.*(?=\44)\45(x*))(?=\46\44*$)))((?=.*(?=\42$)\6(x*)|(x))(?=(\48\49))\42\35\49((?=.*(?=\50$)\41$)\40$|(?=(\40\40\41)+(x*))(?=.*(?=\52)\50\53(x*))\54\52*$)))(?=.*(?=\42$)\6$)\47\35)+$`](https://chat.stackexchange.com/transcript/message/49026359#49026359) \- this works by pair-encoding variables.
With a little modification, it seems it should be possible to design a function that is not possible without `(?*)`. But could it be proven to be impossible?
  4. Are there any functions that can be implemented with right-to-left evaluated variable-length lookbehind `(?<=`...`)`, as in the ECMAScript 2018 standard, but not with `(?*`...`)`? In most ways lookbehind gives a superset of molecular lookahead's power. `^(?*A)B` can be transformed to `^A(?<=(?=B)^.*)`. When we're not at the $tail=n$ position, `(?*A)B` can become `(?<=(.*))A(?<=^\1(?=B).*)`. Lookbehind can directly do things that molecular lookahead cannot; most notably, `^`...`(?:`...`(?<=(?=A)^.*)`...`)*`...`$` uses it to do tests directly on $n$ from within a loop. Are there functions that require this functionality, and can't be implemented with just `(?*`...`)`?
  5. Are there functions that can be implemented with `(?*`...`)` but not with `(?<=`...`)`? Although the latter may seem at first to offer a strict superset of the former's functionality, this may not be the case. `(?:(?*A)B)*` can only be transformed to `(?:(?<=(.*))A(?<=^\1(?=B(.*)).*).*(?=\2$))*` if `A` never advances the cursor by more than `B`.

@Nayuki That logic doesn't work. The equivalent of only a finite number of digits of the transcendental number need to be calculated. By your logic, it wouldn't even be possible in a Turing-complete language to take a natural number N as input and calculate the first N digits of pi as output, but that is possible. (You may be falsely equating transcendental numbers with uncomputable numbers.)

"Can any transcendental number computed": No, because transcendental numbers are uncountable whereas program code strings are countable.


Choosing a subset of binary variables to maximize the sum of the highest $K$

Consider the following problem:

**Input:**

  * integers $n > m > k$;
  * $n$ numbers $0 \leq p_1, \ldots, p_n \leq 1$;
  * $n$ numbers $r_1, \ldots, r_n$ where ($r_i \geq 0$).

Let $X_1,\dots,X_n$ be $n$ independent random variables with distribution $X_i \sim \text{Bernoulli}(p_i)$ and define $Y_i = r_iX_i$.

**Output:** a subset $S$ of $[n]$ of size $m$ that maximizes $r_S = \mathbb{E}[Z_S]$, where $Z_S$ is the random variable $Z_S = \max_{T \subseteq S, |T|=k} \sum_{i\in T} Y_i$.

In other words, we want to select $m$ $Y_i$s such that the expected value of the sum of the largest $k$ of them is maximized.

Can this problem be solved in polynomial time?
Or is it NP-hard?
Is there an efficient $\alpha$-approximation algorithm?

* * *

Example:
**Input:**

  * $n=3, m=2, k=1$
  * $p_1=1.00, p_2=0.10, p_3=0.01$
  * $r_1=2, r_2=11, r_3=100$

**Output:** the correct output is $S = \\{1,3\\}$. Why?

  * $r_{\\{1,3\\}} = 2.98$: if $X_3=1$, then $Z_S=100$ (since $T=\\{3\\}$ in this case), otherwise $Z_S=2$ (since $T=\\{1\\}$ in this case), and thus

$$r_{\\{1,3\\}} = \Pr[X_3=1] \times 100 + \Pr[X_3=0] \times 2 = 0.01 \times 100 + 0.99 \times 2 = 2.98.$$

  * $r_{\\{1,2\\}} = 2.9$: if $X_2=1$, then $Z_S=11$, otherwise $Z_S=2$, and thus

$$r_{\\{1,2\\}} = 0.1 \times 11 + 0.9 \times 2 = 2.9.$$

* * *

I have been working on this for a while now. As the example above demonstrates, the obvious greedy approach of selecting $Y_i$s with largest expectation does not work. I solved the special case of $k=1$ using dynamic programming. But my guess is that the general case is NP-hard.

Note that the value of of $Z_S$ depends on $S$ and the values of $Y_1,\dots,Y_n$: given $S$, the random process is that we randomly pick the values of the $X_1,\dots,X_n$, compute the values of $Y_1,\dots,Y_n$, then based on the values of $Y_1,\dots,Y_n$ we find the set $T$ of size $k$ that maximizes $\sum_{i \in T} Y_i$, and let $Z_S$ be the result (i.e., we let $Z_S$ be the sum of the $k$ largest values from $Y_1,\dots,Y_n$). This is why just choosing the $k$ $Y_i$'s whose expected value is largest does not yield the correct result.


Covering a complete graph with n copies of an arbitrary graph: NP-complete?

> Given a complete graph $G$, an arbitrary graph $H$, and a positive integer $n$, are there subgraphs $A_1,\dots,A_n$ of $G$ (not necessarily disjoint) such that their union is $G$, and each of them are isomorphic to $H$?

This is a problem which I believe is NP-complete, but I am unsure if it is actually so. Any ideas of how to prove this?

Notes:

  * If $G$ is instead allowed to be an arbitrary graph, then this is clearly at least as hard as [subgraph isomorphism](https://en.wikipedia.org/wiki/Subgraph_isomorphism_problem), which is NP-complete.
  * If $G$ is allowed to be an arbitrary graph, and $A_1,\dots,A_n$ are required to be isomorphic to a subgraph of $H$, instead of the entire graph $H$, then the problem can be reduced from [vertex cover](https://en.wikipedia.org/wiki/Vertex_cover) (which is NP-complete) by setting $H$ to be a [star graph](https://en.wikipedia.org/wiki/Star_\(graph_theory\)) with the number of spokes equal to the number of nodes in $G$.
  * The problem where $H$ must also be a complete graph is called "covering design", and has some discussion [here](https://math.stackexchange.com/questions/1734855/minimum-number-of-subsets-of-a-of-a-given-order-that-contain-all-possible-pair), with closed forms for when $H$ has 3 or 4 nodes. Finding algorithm for this is apparently an open problem.

Do you mean a subgraph by a subgraph induced by a subset of vertices of $G$ (which is the definition used in subgraph isomorphism problem)? How do you define the union of two subgraphs?

Another special case that looks hard (an open problem since 1967) is when $H$ is a collection of vertex-disjoint cycles: en.wikipedia.org/wiki/Oberwolfach_problem

@WillardZhan I didn't think about it before. (I mainly thought about cases where $H$ is nearly as large as $G$.) Let's say I'm interested in  answers of either variant.

If $G=K_m$, is $m$ given in binary or unary?