Strikethrough and Roman numeral analysis in Schoenberg

Question: I've bumped into this very brief analysis in Structural Function of Harmony by Schoenberg and I've found that strikethrough V. Now, the chord is an D diminished seventh chord, leading to the dominant and the symbol seems the one used for secondary dominants, despite the (fallible) fact that I know one usually would write II instead of V, since we are in C major.
Where am I wrong? Is there a kind of Schoenberg's notation I'm missing? I searched through glossaries but I found nothing.
Comment: A secondary dominant, as in D7 leading to G in key C, is more often named V/V. I like II, but it's not usual.
Comment: The diminished chord is a B diminished, rather than D. It's the vii chord of C, not a predominant of G. A D diminished chord would be spelled D-F-Ab-Cb.
Answer: V means altered V: Schoenberg is analysing this functionally, and there is no real change of function here. The diminished seventh here is acting both as an appoggiatura to V (A♭ to G, F to G in the bass), and as a form of V itself (dominant minor ninth with the root truncated).
Edit: He's also signalling that we're in C minor - that's what the lower case c means.
Comment: @alephzero, that's more or less what I said, no? :D More seriously, vii°7, when used normally, almost always acts like V♭9.
Comment: I'm inclined to say not really. Generally Ic only shares the bass with V - you can consider it a double appoggiatura of sorts (which is maybe the best way of looking at it) or a kind of predominant, but Ic doesn't generally *act* like a dominant in its own right, so it is hard to call it altered V. In the Götterdämmerung example, the voice leading of vii°7 is set up as an appoggiatura (or accented auxiliary in the second instance, I suppose), but the notes are all part of a dominant ninth anyway - the only thing missing is the root. <*more*>
Comment: As for the key, I'd have to check the context to be 100% sure (although I think we can trust Schoenberg to get it essentially right), but the A♭ strongly suggests a move into the minor. Using the scale degree ♭6 in a dominant-style chord doesn't absolutely guarantee being in the minor mode, but I think we can trust that AS took the context into account. In minor mode cadences in classical music, V (rather than v) is the norm - it provides the leading tone (B♮ in this case) needed for a strong cadence to the tonic.
Comment: I'm no expert on the notation, but it strikes me (no pun intended!) that the first chord is *functionally* some sort of dominant chord, but it doesn't actually *contain* the dominant (i.e. the note G). Whatever it *looks* like, to me it doesn't *sound* like a supertonic chord or a "dominant of the dominant" - it's a dominant chord with some appoggiaturas, which by happenstance doesn't contain its root..
Comment: So, if I understand this correctly, it's like calling the typical IV-Ic-V-I cadence just IV-V(crossed)-V-I, to signify that the function is still that of a V chord, with little difference, am I right?
Regarding that "c", I thought it is standing for a minor key, too, but then GMaj suggested me it was CMaj, but I think it's not so easy.
Comment: Oh, those are really good points! So Ic is just an appoggiatura and has no dominant role itself while the vii°7 chord has dominant role, since its leading tones tend to move towards the I chord (and in fact is part of a dominant ninth). Moreover, sure, making the Bb a B♮ is typical, and so having V, which is stronger than v. Finally, I was not doubting AS =D I only thought he could have used a different notation, which could still be possible, but, as you say, we would need the context.
Answer: Look up Walter Piston's Harmony textbook; this is a "Rootless" Flat-Nine Chord, built upon a second inversion dominant.
Comment: I changed the end to "second inversion dominant"; feel free to roll it back if this is not what you meant.


Bigquery Python client : how is it possible to know if my select query returns any data with a destination table in my configuration?

Question: I'm encountering an issue while trying to determine whether a SELECT query in the BigQuery Python client returned any rows before inserting them into a destination table. Here's a simplified version of the code I'm using
from google.cloud import bigquery
import time

client = bigquery.Client()

query = """
-- SELECT col1, col2 FROM project_id.dataset_id.source_table WHERE col2 is not null
"""

job_config = bigquery.QueryJobConfig(destination="project_id.dataset_id.destination_table")

query_job = client.query(query, job_config=job_config)
while query_job.state != "DONE":
    time.sleep(1)
    query_job.reload()

results = query_job.result()
total_rows_returned = results.total_rows

if total_rows_returned == 0:
    print("Query did not return any rows.")
else:
    print(f"Query returned {total_rows_returned} rows. Inserting into the destination table.")

The issue is that total_rows_returned = results.total_rows seems to return all rows from the destination table instead of the rows returned by the original SELECT query.
I've tried various approaches, including checking num_rows, num_dml_affected_rows, and total_rows from both query_job and results, but none of them provide the expected information.
Is there a reliable way to determine whether the SELECT query returned any rows before inserting them into the destination table in the BigQuery Python client?
Any help or suggestions would be greatly appreciated!
Answer: you can use the total_rows attribute of the query_job object itself. Here's how you can modify your code:
from google.cloud import bigquery
import time

client = bigquery.Client()

query = """
-- SELECT col1, col2 FROM project_id.dataset_id.source_table WHERE col2 is not null
"""

job_config = bigquery.QueryJobConfig(destination="project_id.dataset_id.destination_table")

query_job = client.query(query, job_config=job_config)
while query_job.state != "DONE":
    time.sleep(1)
    query_job.reload()

# Check if the query returned any rows
if query_job.num_rows == 0:
    print("Query did not return any rows.")
else:
    print(f"Query returned {query_job.num_rows} rows. Inserting into the destination table.")

I replaced results.total_rows with query_job.num_rows. This should give you the number of rows returned by the original SELECT query before inserting them into the destination table.
Comment: well 'QueryJob' object has no attribute 'num_rows'
Comment: @fancyeffective try to use total_rows instead, but keep in mind that the value might be 'None', Let me know, if it works I will update the answer to help others.
Answer: Despite exploring various methods, I have not found a straightforward and clean solution.
As a workaround, I use a solution that involves obtaining the total number of rows in the destination table both before and after executing the query. If the count before and after the query is the same, I infer that the SELECT query did not return any rows.
Here's a simplified version of the approach:
 try:
     table = client.get_table(destination_table)
     rows_before_operation = table.num_rows
     query_job = client.query(query, job_config=job_config)
     while query_job.state != "DONE":
         time.sleep(1)
         query_job.reload()
     #does other staff with the job_query

     table = client.get_table(destination_table)
     rows_after_operation = table.num_rows
     if rows_before_operation == rows_after_operation:
         raise QueryReturnsNone("custom message")


How to set environment variables in JUnit 5 for test case?

Question: I am working with JUnit 5. My Java code uses the  System.getenv("demoVar") to access environment variables.
How do I set up this environment variable in a JUnit 5 test class so that my code can access its value during the test?
Comment: With Java 8, you could change your method that calls System.getenv() so that it uses a Function instead. In your production code, you would use System::getenv, and in your test code, you can use Map::get on an internal map that you filled with test values.
Answer: From this other SO answer https://stackoverflow.com/a/59635733/2185719:

There is JUnit Pioneer, a "JUnit 5 extension pack".

jUnit Pioneer offers an annotation that sets environment variables for a test. For example:
@Test
@SetEnvironmentVariable(key = "PATH", value = "")
void testPath_isEmpty() {
    assertThat(System.getenv("PATH")).isEmpty();
}

Comment: This would give a warning from JDK9-16 and throws InaccessibleObjectException in JDK17.
Comment: There's an issue in the library's repository discussing the warnings and errors around using this annotation. It also offers ways to work around it. https://github.com/junit-pioneer/junit-pioneer/issues/509
Comment: Thanks for the info @Miguel Ferreira. (They are still work arounds, we are planning to remove env variable dependency instead of using the work arounds). Thanks again for your time
Comment: @Bala Pioneer dedicated an entire section to this "issue": https://junit-pioneer.org/docs/environment-variables/#warnings-for-reflective-access
Answer: You can't within the actual java process because these environmental values using getenv are immutable.
One way would be to start another vm or another process where you could introduce your new environment value.
Another way would be to switch to System.getProperty, but be sure you understand the differences.
https://www.baeldung.com/java-system-get-property-vs-system-getenv
Here is a little testcode:
public class EnvironmentVarsTest {
    private static int counter = 0;

    @BeforeEach
    public void setUp() {
        counter = counter + 1;
        System.setProperty("simple_test_env_property", String.valueOf(counter));
    }

    @Test
    public void testFirst() {
        printOutValues();
    }

    @Test
    public void testSecond() {
        printOutValues();
    }

    private void printOutValues() {
        System.out.println("--------------------");
        System.out.println("val=" + counter);
        System.out.println("envval=" + System.getProperty("simple_test_env_property"));
    }

}

Comment: There is a [hack](https://stackoverflow.com/a/7201825/74334) that lets you set environment variables for the current process from Java code. It's not pretty and you shouldn't use it unless you really need it. On the other hand, Java should really have a proper method to do this without resorting to hacks.
Comment: Note: there are libraries that let you do this easily. However, from Java 16 onwards, the reflection hacks no longer work. https://github.com/webcompere/system-stubs has a mockito-based solution to this which does work.
Answer: This can be achieved with https://github.com/webcompere/system-stubs/tree/master/system-stubs-jupiter

@ExtendWith(SystemStubsExtension.class)
class TestClass {

    @SystemStub
    private EnvironmentVariables environmentVariables =
        new EnvironmentVariables("demoVar", "val");

    @Test
    void test() {
        // can use the environment
        assertThat(System.getenv("demoVar")).isEqualTo("val");

        // can also change the environment
        environmentVariables.set("foo", "bar");

        // environment variables restored to previous state when
        // test ends
    }
}

Comment: does not work with kotlin v1.9 and java 17
Comment: Please raise an issue on the github project. It definitely can work with Java 17, though won't survive multi-threaded code.
Answer: Simple solution if you use Gradle, you can add following to your build.gradle
test {
  environment "ENV_VAR_NAME", "ENV_VAR_VALUE"
}

Link to Gradle doc: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:environment
Answer: The common practice is to use System properties instead of environment variables.
In this case you will run your java/maven/gradle command or whatever you use to run your tests with option -D demoVar="{your_value}".
for maven goal:
 maven clean install -DdemoVar="test"

for java jar:
 java -jar xxx.jar -DdemoVar="test"

You will be able to get it from code with System.getProperty("demoVar").
If you really need to use environment variable, use OS functionality.
For linux:
demoVar="test" mvn clean install

For windows PowerShell:
$env:demoVar = 'test'; mvn clean install

Comment: Thanks! ```$env:demoVar = 'test'; mvn clean install``` - quick simple and works
Answer: Here is a solution using Kotlin DSL (build.gradle.kts):
tasks.withType<Test> {
    environment["myFirst"] = "bunny"
    // OR
    environment(
        "mySecond" to "7249",
        "myThird" to "curtain"
    )
}

The environment variables will be available both in the tests and in the main code:
val name = System.getenv().get("myFirst")

Sidenote: You can also disable/enable your tests based on environment variables:
@Test
@EnabledIfEnvironmentVariable(named = "myFirst", matches = "bunny", disabledReason = "Oh no")
public void myTest() {
    // ...
}


capture all consecutive values varying from 2 to 11 entries in R

Question: Recently @AntoniosK helped me figure out how to capture the first and last value of consecutive data points using the following code.
library(tidyverse)

f = function(x) {

DF %>%
gather(M,First,-ID) %>%                 # reshape data
group_by(ID) %>%                        # for each ID
mutate(Last = lead(First, x-1)) %>%     # get the next values in a new column
ungroup() %>%                           # forget the grouping
filter(First != "." & Last != ".") %>%  # exclude rows with . as value
arrange(ID) %>%                         # order ID (needed for visualisation purposes only)
select(-M)                              # remove unnecessary column
}

RDF <- data.frame(consec_months = 2:11) %>%
mutate(d = map(consec_months, ~f(.))) %>%
unnest() %>%
tbl_df()       # only for visualisation purposes

This was great and exactly what I wanted.  However, I want all of the values in between for another variable.
An example:
ID M2  M3  M4  M5  M6  M7  M8  M9  M10 M11 M12 M13
1   5   15  2   20  .   .   .   4   2   7   8   .
2   2   8   7   6   5   4   7   4   7   9   9   8
3   .   .   .   .   .   .   .   .   .   .   7   7
4   6   7   8   6   .   .   .   .   .   7   6   5

The 3 consecutive answer for ID 1 would be the following:
ID  cons_months M1
1   3   5   15  2
1   3   15  2   20
1   3   4   2   7
1   3   2   7   8

Answer: df = read.table(text = "
ID M2  M3  M4  M5  M6  M7  M8  M9  M10 M11 M12 M13
1   5   15  2   20  .   .   .   4   2   7   8   .
2   2   8   7   6   5   4   7   4   7   9   9   8
3   .   .   .   .   .   .   .   .   .   .   7   7
4   6   7   8   6   .   .   .   .   .   7   6   5
", header=T, stringsAsFactors=F)

library(tidyverse)

# create function that gets l as input (number of consecutive values) and returns a dataframe with those values as columns
f = function(l) {

  df %>%
    gather(M,First,-ID) %>%           # reshape data
    arrange(ID) %>%                   # arrange by ID
    group_by(ID) %>%                  # for each ID
    mutate(vec = list(First),         # save First vector as vec
           pos = row_number()) %>%    # save the position of this element in that vector
    ungroup(ID) %>%                   # forget the grouping
    filter(First != ".") %>%          # remove . values
    mutate(values = map2(pos, vec, ~data.frame(t(.y[.x:(.x+l-1)]), stringsAsFactors = F)), # save consecutive values based on position of that element and the corresponding vector
           flag = map(values, ~sum(.=="."))) %>%     # count how many . exists in those consecutive values
    unnest(values, flag) %>%                         # unnest data
    filter(flag == 0) %>%                            # keep consecutive values with no .
    select(ID, matches("X"))  }                      # keep columns of interest

# apply the function
data.frame(consec_months = 2:11) %>%
  mutate(d = map(consec_months, ~f(.))) %>%
  unnest() %>%
  tbl_df()

# # A tibble: 87 x 13
#   consec_months    ID X1    X2    X3    X4    X5    X6    X7    X8    X9    X10   X11
#           <int> <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
# 1             2     1 5     15    NA    NA    NA    NA    NA    NA    NA    NA    NA
# 2             2     1 15    2     NA    NA    NA    NA    NA    NA    NA    NA    NA
# 3             2     1 2     20    NA    NA    NA    NA    NA    NA    NA    NA    NA
# 4             2     1 4     2     NA    NA    NA    NA    NA    NA    NA    NA    NA
# 5             2     1 2     7     NA    NA    NA    NA    NA    NA    NA    NA    NA
# 6             2     1 7     8     NA    NA    NA    NA    NA    NA    NA    NA    NA
# 7             2     2 2     8     NA    NA    NA    NA    NA    NA    NA    NA    NA
# 8             2     2 8     7     NA    NA    NA    NA    NA    NA    NA    NA    NA
# 9             2     2 7     6     NA    NA    NA    NA    NA    NA    NA    NA    NA
# 10            2     2 6     5     NA    NA    NA    NA    NA    NA    NA    NA    NA
# # ... with 77 more rows

Obviously, different number of steps means different number of values stored in columns. Therefore, some columns will have NA values if you want to store everything in one table.
Comment: Nailed it again.  Thank you so much.

I can just differentiate with that consec_months variable and be good to go
Comment: Let me know if I can update my answer in a way that helps you more. You can vote or accept the answer if you like it :)
Comment: How would I plot the differences of the mean of each column?  mean(X2) - mean(X1) and so on.


Why I can't effect READ_UNCOMMITTED Isolation in my test

Question: I's just want to test the Transactional annotation support by spring with spring data jpa,and spring boot.
Here's my controller method.
@GetMapping(path = "testreaduncommit")
public void testReadUnCommit()
{
    new Thread(new Runnable() {
        @Override
        public void run() {
        testService.testReadUncommitted1();
        }
    }).start();
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    testService.testReadUncommitted2();
}

And There is my service implement
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void testReadUncommitted1() {
    log.info("testUncommit1 begin");
    User u=userRepository.findOne(1L);
    u.setUserPwd("654321");
    userRepository.save(u);
    log.info("testUncommit1 sleep");
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    log.info("testUncommit1 will be commit");
}

@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void testReadUncommitted2() {
    log.info("testUncommit2 begin");
    User u=userRepository.findOne(1L);
    log.info(u);
}

And the result is:
testUncommit1 begin;
testUncommit1 sleep;
testUncommit2 begin;
User(userId=1, userName=testuser, userPhone=15888888888, userStatus=2, userPwd=123456);
testUncommit1 will be commit;
So I wonder why the annotaion @Transactional's property 'isolation = Isolation.READ_UNCOMMITTED' don't effect,I think the result should be userPwd=654321 because there should be 'dirty read' occur for the READ_UNCOMMITTED.
And I had try to add userRepository.flush();,It effected but after I change the isolation level to SERIALIZABLE,the dirty read happened too,why?
My database is Mysql.
So when I add the flush();
I change the method's isolation;
@Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void testReadCommit1() {
        log.info("testReadCommit1 begin");
        User u=userRepository.findOne(1L);
        u.setUserPwd("654321");
        userRepository.save(u);
        userRepository.flush();
        log.info("testReadCommit1 sleep");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("testReadCommit1 will be commit");
    }

    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void testReadCommit2() {
        log.info("testUncommit2 begin");
        User u=userRepository.findOne(1L);
        log.info(u);
    }

The result is:
User(userId=1, userName=testuser, userPhone=15888888888, userStatus=2, userPwd=654321)
Why SERIALIZABLE didn't prevent the dirty read?
Comment: What database are you using?
Comment: Data Base Mysql
Comment: you have to call flush because save does not execute the SQL statements. Please update your question that I can understand what your problem after adding flush is.
Comment: I've edit the question, thanks for your help
Comment: Please edit your question correctly. READ_UNCOMMITED works no after adding flush() but SERIALIZE doesn't work right?
Answer: Following API docs says call to flush() will flush all pending changes to the database. And hence when your testReadCommit2() method runs after 3 seconds, it isn't reading the dirty record.
https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html#flush--


I'm looking for information/references on how we developed the Major Mode (Scale/Key)

Question: As the title says, I'm looking for some information on the history of how the Major Mode (Scale,Key) was developed/designed. I already know a little bit, that is, I am not completely new to the topic, but I wanted to develop a better understanding of how we arrived at the Major Mode. Specifically the history, and the theory behind how it was developed/designed. I notice there is some nuance to it. Some theorist say it comes from the overtone series.
https://en.m.wikipedia.org/w/index.php?oldid=871485588&title=Ptolemy%27s_intense_diatonic_scale
This link says it can be derived from the major chord and the major chords above and below it, which is also touched on by Schoenberg. I'm wondering if anyone knows of any other theorists discoveries, such as that of Ptolemy and Zarlino, about the Major Mode & Minor Mode. And to expand on that, about  the development/design of chords (tetrachords, hexachords etc), & modes in general. I'd appreciate answers with references, so I can do further researching. I have a large collection of books, as I like to have all of the valuable information in print.
Thank you in advance!
Comment: Also, heads up, like many things it wasn't really "designed" in one fell swoop with the goal of becoming what it is today; it evolved over time; e.g. Pythagoras was more interested in the tetrachord than in stacking them to make an 8-note scale.
Comment: Meanwhile I wouldn't put much stock in the Wikipedia sentence describing the scale as "derived from the major chord." That may be a helpful way to think about it, but doesn't describe the order of historical events. "Chords" as a concept came centuries (lots of centuries!) later than tetrachords, scales, and modes.
Comment: Welcome! Requests for outside resources [aren't covered here](https://music.stackexchange.com/help/on-topic); the idea is that future users should be able to benefit from the same answers without hunting other stuff down. This question is one vote away from being closed, but I feel like you have a perfectly valid question with a bit of editing. I recommend downplaying the request for books and just ask for the info you need *here*, maybe including "I'd appreciate answers with references so I can research further."
Comment: I've retracted my vtc, as now you're asking a question which deserves a good answer, rather than a list of writings on said subject.
Comment: @AndyBonner Hey, oh ok thanks gotcha! I've edited my question, I hope that covers the issue! Also, I understand that it was developed, I just slightly used the wrong word. And lastly, interesting now I am wondering why he was more interested in the tetrachord than in stacking them. But thank you, I will look into that now as well!
Comment: @Tim Good, thanks! Yea Andy cleared it up for me, I didn't realize I was doing something wrong lol!
Comment: I've voted to close, since the only answers generated are lists of references.
Answer: Check out Big Bangs - The story of five discoveries that changed musical history - by Howard Goodall, published by Vintage Books.
It's not specifically about how we arrived at modern major scales, but it's an excellent starting point.
Comment: Thanks I've read a little bit so far and I see what you mean about it being an excellent starting point!
Answer: This paper has a good discussion of the development of major and minor keys. The major scale (as an ordering of notes) had been around much earlier.
https://www.academia.edu/291944/A_rudimentary_approach_to_the_history_of_the_major_and_minor_keys
Newly added references:
Carl Dalhaus Studies on the Origin of Harmonic Tonality Joel Lester:  Compositional theory in the eighteenth century
https://soundideas.pugetsound.edu/cgi/viewcontent.cgi?referer=&httpsredir=1&article=1079&context=summer_research
Comment: Thanks! I'm gonna read some more of this today
Comment: Some more useful books. Carl Dalhaus Studies on the Origin of Harmonic Tonality.

I’m looking for information/references on how we developed the Major Mode (Scale/Key)


why is there a division by zero in the formula of the characteristic equations for a homogeneous first order pde ??

Question: I often see in some solutions of first order homogeneous pde problems like this one for example : $\frac{\partial u}{\partial x}+3y\frac{\partial u}{\partial y} =0$
where : $u=u(x,y)$ , that the characteristic equations are given by :
$\frac{dx}{1}=\frac{dy}{3y}=\frac{du}{\color{red}0}$
How could we divide by zero ???
Answer: It's a purely formal statement. The argument is that an infinitesimal displacement along a curve into the surface solution $(dx,dy,du)$ is set proportional to $(a,b,c)$ for a quasilinear ODE $a\dfrac{\partial u}{\partial x}+b\dfrac{\partial u}{\partial y}=c$. So is: $(dx,dy,du)=k(a,b,c)$ or
$$\dfrac{dx}{a}=\dfrac{dy}{b}=\dfrac{du}{c}$$
If $c=0$, from the original condition we have $du=0$, so $\dfrac{du}{0}$ in fact is telling that $du=0$
It the same as for the continuous equation for a straight line: $\dfrac{x-x_0}{v_x}=\dfrac{y-y_0}{v_y}=\dfrac{z-z_0}{v_z}$, being $(v_x,v_y,v_z)$ a vector along the line. In the case with $v_z=0$ we still write $\dfrac{x-x_0}{v_x}=\dfrac{y-y_0}{v_y}=\dfrac{z-z_0}{0}$, but we only mean the $z-$component of the vector along the line is zero. We like to write proportions whenever we can, or even when we cannot!
Answer: If you want to think rigorously, best to stop agonizing about differentials. They are useful enough, but at some point, you want to think about the method of characteristics being really finding a path on which a PDE becomes an ODE:
\begin{align*}
\frac{du}{ds} = \frac{dx}{ds} \frac{\partial u}{\partial x} + \frac{dy}{ds} \frac{\partial u}{\partial y} = 0
\end{align*}
so for your particular case
\begin{align*}
\frac{du}{ds} = 0, \quad \frac{dx}{ds} = 1, \quad \frac{dy}{ds} = 3y
\end{align*}
so that
\begin{align*}
u = c_1, \quad x = s + c_2, \quad y = c_{3}\exp(3s),
\end{align*}
so for instance, $ye^{-3x}$ is constant on solutions. This gives
\begin{align*}
u = f(ye^{-3x})
\end{align*}
No differentials required.


SVG Animation Jumping

Question: I'm beginning to just learn some SVG (and javascript, I suppose), and I'm having a hard time understanding why this is not flowing smoothly. The 0 moves a small amount (presumably "1" horizontally on the x axis), but than begins to leap in great bounds. Is this because the browser I am using (Chrome) is refreshing/redrawing at a much longer pace? Any input would be greatly appreciated.
 <svg width="100%" height="100%"
    xmlns="http://www.w3.org/2000/svg" version="1.1"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    onload="startup(evt)">

<defs>
    <text id="zero">0</text>
</defs>
<use x="40" y="20" xlink:href="#zero"/>
</svg>

Answer: You have to make sure you're working with numbers and not strings:
var x = +(e.getAttribute("x"));
e.setAttribute("x",x+1);

JavaScript prefers string "addition" to numeric, so when "x" is a string, x+1 means "add the character '1' to the end of the string x". By forcing the return value from .getAttribute() to numeric via the unary "+" operator, you avoid that problem.
(There are various ways to force a string to be converted to a number. Pick your favorite :-)

SVG Animation Jumping


Como limpar a tela do interpretador de Python do Windows?

Question: Estou começando a estudar Python (versão 3), fazendo vários testes de sintaxe utilizando o interpretador para Windows. Porém, gostaria de limpar da tela os comandos que já executei, à medida que a barra de rolagem vai crescendo. Tentei digitar clear, clean, CTRL+L, mas nada disso funcionou.
Answer: Com base nestas fontes (fonte1, fonte2), sendo windows podes fazer:
import os
os.system('cls')

Se fosse linux seria:
import os
os.system('clear')

Não testei, mas acredito que resulte
Comment: Funcionou. Mas achei um pouco verboso para um simples comando de clear...
Comment: É verdade também acho, mas é windows :P, vou tentar encontrar alguma alternativa mas duvido que haja uma menos verbosa
Comment: Realmente, não há. Até porque o comando executado pertence ao interpretador, não é nativo ao Python, portanto deve-se utilizar a biblioteca `os`. Lembre-se também que Python é portável: o mesmo código funciona tanto em Windows quanto em Linux, ou outro sistema operacional, então é interessante fazer algo como `os.system('cls' if os.name=='nt' else 'clear')`, pois `cls` funciona no Windows, enquanto `clear` funciona no Linux.
Comment: Acrescentei essa alternativa (sendo linux) à resposta, obgado @AndersonCarlosWoss


Menu Dropdown do Bootstrap em site asp.net funciona apenas na página principal, o que fazer para corrigi-lo?

Question: O menu dropdown aparentemente funciona na página principal, mas quando estou em outra página e vou utilizá-lo ele não funciona! Já verifiquei se no código HTML está gerando com o jQuery e não vi nada de estranho. O que fazer?

               sis

                        Cadastros

Consulta de Pessoa Fisica

Comment: Tem alguns erros no seu código, falta fechar algumas tags, e está faltando o `bootstrap.min.css`. E não precisa colocar o `bootstrap.js` e o `bootstrap.min.js` (só o `.min.js` já basta, a mesma coisa com o jQuery). Este menu é pra ser de três níveis? Tipo menu>cadastro>consulta de pessoa fisica (3 níveis) ou o menu aparece ao lado mesmo, e o consulta de pessoa física está dentro de Cadastro (2 níveis)? Dá uma olhada se é mais ou menos isso que vc precisa:  http://jsfiddle.net/chirayu45/yxkut/16/
Comment: Não tem um nível mesmo. Quanto ao bootstrap.min.css ele está no header da Masterpage. O problema é que quando eu estou por exemplo no form de Consulta, o DropdownMenu não funciona. A URL fica assim:
http://localhost:1078/PessoasFisica/frmConPessoaFisica.aspx#
Comment: Acho que a resposta do @dHEKU vai solucionar. Veja o exemplo que ele postou no Bootply, e se isso resolver seu problema, não esqueça de aceitar a resposta dele (é assim que agradecemos por aqui ;)).
Comment: O meu bootstrap funcionando apenas na primeira página, então não adianta eu colocar o meu código no http://www.bootply.com/Y60uWy24UY, pois ele só irá mostrar o bootstrap funcionando normalmente. O meu problema está na URL do ASP.NET que não está alterando quando estou em outra página.
Answer: Você não precisa colocar o jquery de ativação do dropdown a não ser que queira utilizar algum evento em conjunto com o clique ou qualquer outra coisa - Só com formatação você consegue colocar ele para funcionar. Retire também os js externos não necessários:
http://www.bootply.com/Y60uWy24UY

            sis

          Cadastro

Consulta de Pessoa Fisica

Another action

Something else here

Comment: O meu bootstrap funcionando apenas na primeira página, então não adianta eu colocar o meu código no http://www.bootply.com/Y60uWy24UY, pois ele só irá mostrar o bootstrap funcionando normalmente. O meu problema está na URL do ASP.NET que não está alterando quando estou em outra página.
Comment: Então é um problema na estrutura do seu MVC, verifique se o plugin jquery do bootstrap esta sendo carregado em todas as páginas.
Comment: O meu projeto é WebForms, talvez faça a diferença. Já utilizei em MVC e funcionou perfeitamente.


Issues Using Midl to create a .tlb from .idl "expecting a type specification"

Question: I have been having a prolonged encounter with the beast known as COM Interop...
I am currently trying to generate a .tlb from a .idl file generated by the OLE/COM Object Viewer. However when trying to run Midl.exe to compile it I get an error:
.\Sim.API.IDL(236) : error MIDL2025 : syntax error : expecting a type s
pecification near "ImportFileStatus"

My .idl file is more that 1000 lines long so I don't particularly want to post it here however, I believe the Part of interest is:
typedef [uuid(980B172E-19C1-389A-BB74-29A54737C5B4), version(1.0)    ,
  custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Sim.API.ImportFileResult")
 ]
 struct tagImportFileResult {

  ImportFileStatus _status;

  LPSTR _message;
 } ImportFileResult;

Then Several lines later...
 typedef [uuid(A4B9A0FF-A2D4-3EC5-AB7E-69311B9122C8), version(1.0)    ,
  custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Sim.API.ImportFileStatus")
 ]
 enum {
  ImportFileStatus_Success = 0,
  ImportFileStatus_VersionMismatch = 1,
  ImportFileStatus_Failure = 2
 } ImportFileStatus;

I have a Feeling that these should be revered in order to fix the Type specification error. However if I do this I get a new problem.
midl\oleaut32.dll : warning MIDL2368 : error generating type library, ignored :
Could not set UUID : tagImportFileResult (0x800288C6)

I am pretty unfamiliar with the idl format and with the use of midl.exe, perhaps there is something blatantly wrong with what I am doing?
As always any help would be greatly appreciated :)
Answer: You are correct, swapping the declarations is required to keep MIDL happy.  OleView.exe indeed won't generate declarations in the original order.  I think it groups them by kind, the way the type lib is organized.
The message you are getting is just a warning, not an error.  It is caused by having an alias for the structure name that's different.  You can safely ignore it because code won't use the "tagImportFileResult" identifier.  But you can get rid of it by making the tag name the same as the typedef name:
typedef [..]
   struct ImportFileResult {
   //...
} ImportFileResult;

Here's a KB article on the subject.
Comment: Sure, just ask the original author for his .idl file.  You'll get one out of two possible responses: "no problem!" or "are you kidding, this is copyrighted software, your license expressly forbids you doing this!".  Or you'll never hear anything back, not unlikely given its probable age.
Comment: Actually I am the original author ^^ or at least my company is, Long story but this is basically  to get around the 'propput' rather than 'propputref' Issue when using Object Properties. So I have to generate and register the original .dll with Regasm and then edit the .IDL to replace propputref with propput and finally rebuild the tlb with Midl. FUN!!!! :P
Comment: Thanks Hans, I would never have noticed that the second error was simply a warning :P. Incidentally is there a better way to generate IDL's that does not change the order? Also do you know any other useful resources for COM Interop? Thanks again!


Prove that structure $(Q/{0}; =;*)$ has infinite number of automorphisms

Question: Prove that structure $(Q/{0}; =;*)$ has infinite number of automorphisms
I think it's about we don't have $<$ or $>$, so we don't have strict order in structure, so we can do whatever we want? but i dont know how to say it more formal/
Comment: What is $Q$? (Is it the set of rational numbers, i.e. $\mathbb Q$?) What is $Q/0$? (Did you mean $\mathbb Q\setminus\{0\}$? Finally, what is $*$ ? (Is it multiplication?)
Comment: Yes, you're right. Sorry. My LaTeX skill is not so good.
Answer: Hint: Take any permutation (i.e. bijection to itself) of the set of the prime numbers $P$: $\phi:P\to P$. Now map every rational number $q=\pm p_1^{\lambda_1}\cdots p_n^{\lambda_n}$, where $\lambda_1,\ldots\lambda_n\in\mathbb Z$ into $\pm \phi(p_1)^{\lambda_1}\cdots \phi(p_n)^{\lambda_n}$. Prove that this map is an automorphism.
Comment: Thank you! It helped me a lot!


Term translation in views

Question: i try to create a multilingual page with drupal.
http://alarabi.biz/category-listing
I created some categoreis and translated them with the Internationalization plugin and i18nviews.
So I can chouse the language in views settings

but all I get is language "und" I think it means undefined.
How to fix it? Terms are translated.
Comment: Did you not translate terms?
Comment: yes, I use the  Internationalization > Taxonomy translation module to translate them.
Answer: You need to use the Name (Translated) handler provided by i18nviews.
Answer: You can configure the language settings for the View on the edit page under:
Advanced > Other > Field Language
Comment: this setting has no effect for me

Term translation in views


eigenvalue with singular kernel

Question: I'm working through Lang's Linear Algebra, and a problem I'm working on a problem that has a hypothesis I'm not sure of. Let $ A:V\to V $ by a symmetric linear map. Show that $Ker(A) = \{0\}\ $ if and only if no eigenvalue is zero. My idea is this:
Assume $Ker(A) = \{0\}$. Then $Av = \lambda v = 0$ if and only if $v=0$. So, if $\lambda = 0$, we have that $Aw = 0$ for any $w\in V$. But this contradicts the assumption that the kernel has one element. Hence, $\lambda = 0$. Conversely, suppose $\lambda \neq 0 $. Then if $Av = 0 = \lambda v$, we have that $v=0$, so that $Ker(A) = \{0\} $.
However, nothing in this proof relies on $A$ being symmetric. Did I make an error in my proof, or is $A$ being symmetric an extraneous condition?
Answer: I believe you meant $A: V \to V$, not $L: V \to V$. And yes, the symmetry condition seems redundant because in general, for any $\lambda \in F$, where $F$ is the field we are working with, we have
\begin{align}
\ker(A- \lambda I) \neq \{0\} & \iff \text{there is a non-zero $v \in V$ such that } A(v) = \lambda v \\
& \iff \text{$\lambda$ is an eigenvalue of $A$}
\end{align}
So, by taking the contrapositive, and choosing $\lambda = 0$, we get the statement:

$\ker(A) = \{ 0\}$ if and only if $0$ is not an eigenvalue of $A$
Comment: I did mean $A$, sorry about that! And thank you, this helps

eigenvalue with singular kernel


hex string with zeros left padded

Question: To left pad a number with zeros if there are fewer than two digits we use this:
String.format("%02d", aBlkNo);

However, aBlkNo should be a hex string not a decimal. What can I do to render a two digit hex string from a byte with zeros padded when necessary?
Answer: Use "x" or "X" instead of "d":
String.format("%02x", aBlkNo);

Answer: In Kotlin you can simply add "padStart" to a string/hexString
val paddedValue = "a".padStart(2, '0')
println(paddedValue) // "0a"

hex string with zeros left padded


Latent variables in covariance based structural equation modeling

Question: In the partial least squares approach to SEM the latent variables are a weighted sum of their manifest variables:
$$LV_{1}=w_{1}X_{1}+w_{2}X_{2}+w_{3}X_{3}$$
I think this refers to the composite factor model.
With this I can solve regression equations like:
$$LV_{1}=\beta_{21}LV_{2}+\epsilon_{21}$$
Can anybody tell me what the latent Variables in covariance based structural equation modeling are? I think it is called the common factor model and the latent Variables do not have specific values like in PLS but how are the path coefficients $\beta$ computed then? How do the equations look like? It must be some combination of the correlations among the manifest variables x then right? I am not asking for the algorithm but for the plain equations at the end of the algorithm which give the final path coefficients and outer loadings of a CBSEM model.
Answer: You might want to check out this JStatSoft paper.
In Covariance based models, the structural equations and latent variable models define a particular covariance struture. This is compared to the actual, observed covariance matrix and parameters are estimated to ensure a good fit. Several approaches are possible, but maximum likelihood and least squares are popular choices. The LV models are of the form:
$$
X_1 = \lambda_1 \xi_1 + \epsilon_1 $$
say, if $X_1$ only loads on one factor. More complex models are possible, of course:
$$
X_1 = \lambda_1 \xi_1 + \lambda_2 \xi_2 + \epsilon_1 $$
The $\xi$ are the latent variables and the $\epsilon$'s are the uniquenesses -- We model the observed, manifest variable as a linear combination of unseen latent variables, which affect several manifests, and some noise, which is unique to each manifest. So if $X_1$ is my score on one math test, and $X_2$ is my score on another such test, the idea is that each score is due to my overal math ability (latent variable) and to the vagaries of each test experience.
Regression equations in CBSEM are the same as those used in the partial least squares approach.
In any case, there aren't ``plain equations'' for the path coefficients, since these do not exist in closed form, but are the result of optimizing an objective function -- typically ML or least squares.
I hope this helps.
Comment: So I think I understood in some way, but let me ask another question to verify: If I would estimate the following model with CBSEM http://stats.stackexchange.com/questions/156107/linear-path-models-vs-pls-path-models-structural-equation-models the model would fit the covariance matrix perfect in advance and therefore no optimizing would be needed?
Comment: In that model, the covariance has 21 distinct values, which probably don't correspond to what the model predicts that covariance to be. So you pick the parameters that give the best fit.
Comment: I did not understand this comment, there is a supplied covariance matrix in the first code snippet at the very bottom, which is basically just the paths multiplied so cor(x1,x4)= 0.8*0.8*0.8 = 0.512 and cor(x1,x2) = 0.8*0.8 = 0.64. My question is if I supply CBSEM with that model and the implied covariance matrix, will it estimate all parameters as 0.8 like implied by the model and covariance matrix?
Comment: OK. I get it. I thought you were asking about the original, sample covariance. But yes, the implied covariance would fit the model perfectly.


contains filter in Smarttable using defaultOperationMode Client

Question: my current setup:

Smarttable
personalisation Dialog (for Filter/Sorter)
defaultOperationMode: Client side

Problem
using client side filtering in general works.
But using contains is case sensitive. Why? (is it a framework bug)
filtering using "contains" in smarttable works on "defaultOperationMode": "Auto"

not case sensitive correct

filtering using "contains" in smarttable does NOT work on "defaultOperationMode": "Client"

case sensitive
I have to write the "Cup" with capital letter to find the Cup 1, Cup 2,..

regards
Answer: On the client side, filtering is done in the stored JSON and JSON is case sensitive. Object['field'] is different from Object['Field']. On the server side, you have your database dealing with some of your searches and especially in a HANA CDS this might take care of the case sensitivity of a CONTAINS statement.
If you want case insensitive searches on an old database or client side, you'll have to translate all strings and searches to either upper or lower case yourself I think.
Comment: thanks for the replay. since I am using the oDataV2 my understanding was that the data is still in the oData Model and not in a JSON. even the filter ist the same. but unfortunately not working in client mode. I will have to use your workaround, thank you.
Comment: I should clarify - the data is still in the oData model but that doesn't mean it's not stored as a json object


Show that the following set is a left ideal

Question:
We have $R$ a commutative Ring with neutral Element. $M\subset R^n$ a $R$-submodule. Let $I_m$ be the set of matrices in $M_n(R)$(n matrices with elements in R) whose rows are elements of the submodule $M$. Show that $I_m$ is a left ideal of $M_n(R)$.

I tried taking the ring $R=\mathbb{Z}$ and a lattice in $\mathbb{Z^2}$, composed from 2 random vectors. Then taking  $2\times2$ matrices. $I_m$ would be the set of matrices, whose row are integer combinations of these two vectors and since for the ideal i need for it to be closed under addition and left multiplication with elements of $M_n(R)$, i tried some examples and it worked.
But this is just an example and by no means a proof so I would really need some help.
Thanks in advance
Annalisa
Answer: First of all, this Ideal is a subgroup of $M_n(R)$ as addition of matrices breaks down to addition of vectors $m,n\in M$. As $M$ is an $R$-module, the addition $m+n\in M$.
Now take a matrix $A = \left(\begin{array} & a_1 & a_2 & ... & a_n\end{array}\right)^T\in I_M$ with rows $a_i\in M$.
Now let's take a look at the left-multiplication with another matrix $B \in M_n(R)$ with its rows $b_1,...,b_n\in R^n$. We now want to show that the rows of $B\cdot A$ are elements of $M$:
Let $c_i:=(BA)_i$ be the $i$-th row of the matrix product:
$$
c_i = b_i \cdot A = \sum_{j=0}^n b_{ij} \cdot a_{j}
$$
However, we have $b_{ij}\in R$ and as $M$ is an $R$-module we get $b_{ij}\cdot a_j \in M$. As $M$ is closed under addition we finally have $c_i \in M$ for all rows which shows that $B\cdot A \in I_M$ and $I_M$ is indeed a left ideal.
Comment: Because the $a_i$ are representing the rows of $A$, but without the transpose, they would show the columns. Additionally i was too lazy to write the matrix with the rows directly
Comment: No, $\{0\}$ is a left-ideal as well and it's (in general) not isomorphic to $I_M$
Comment: Very helpful. May i ask, is there any specific reason why we took the transpose?
Comment: Would all other left ideals be isomorphic to our $I_m$
Comment: is then any other relationship between the left ideals and $I_m$?
Comment: I have not that deep knowledge of abstract algebra. However, I don't think you can get a relevant relationship between other left ideals and $I_M$. $I_M$ is the canonical left ideal lifting the $R$-module $M$ to the set of matrices. That's the main thing to know about $I_M$ (at least what I can come up with)


How can I replace the "Add to cart" button with an "Ask about product" button, based on product property?

Question: Let's say client wants button "Ask about" for chosen product instead of "Add to cart" (he requires personal contact for some products). It should be based on data from a specific product field, so he could choose for what products he requires personal contact. It would be also nice if that optional button would redirect user to webform, that would automatically contain name and attributes of chosen product. How can I approach this?
Added:
I can disable "add to cart" form, from hook_preprocess_node, or hook_node_view_alter, but I wonder if it can be ajaxified, so only if user will choose specific product, from configuration of attributes, the "add to cart" button, would disappear and "Ask about product" would appear. I also need to turn off possibility of adding that product to cart by forged POST requests. Anyone know how to do it?
Answer: I figured out answer, and it's not that bad as I though. I decided to write a tutorial for it as it looks like very useful feature. So here it is:
Tutorial

Install everything, I mean, Commerce and Webforms,
Create example webform - let's name it "Contact",
Create Product Display, create some product with few attributes, and one boolean field (not attribute) "Contact required",
Configure webform, specifically, create there a field that will have default value, fetched from GET query string (how to do this: http://drupal.org/node/296453#webform-url-default ), we will pass to our Contact form a SKU of our product by using GET method.

Now create your own module, I will not elaborate on that, as there is a lot of info out there. The hooks that we will need are:

HOOK_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state)
HOOK_commerce_cart_attributes_refresh_alter(&$commands, $form, $form_state)

The first one will allow us to manipulate our form initially (if default product will have "Contact required = true"), the latter will allow us to adjust our form to AJAX calls.
Yes, that means that, our "Add to cart" button will be dynamically changing based on current product "Contact required" field. Looks promising doesn't it? :)
We will also use two helper functions that are not hooks:

MODULE_ask_for_product($product)
MODULE_contact_required_validate($form, &$form_state)

The first one will return HTML of our new button "Ask for product" (I encapsulated it in function, because we will need that data in both above hooks),
and the second one will validate our form, so if "Contact require" field on product, will be set to 1, the form will not validate, and product will not be added to cart. The submit button for regular user will be removed, but this is only against smart guys that would like to annoy you by sending this form even if you hide submit button.
Those are the functions:
    function HOOK_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
    // Adding new class to our button, that will allow us, to target it by jQuery
    $form['submit']['#attributes']['class'][] = 'add-to-cart-button';
    // Adding new validator, that will make sure that our products with required contact, will not be added to cart
    $form['#validate'][] = 'MODULE_is_contact_required_validate';

    // Getting value of field_is_contact_required of default product
    $product = $form_state['default_product'];
    $field = field_get_items('commerce_product', $product, 'field_is_contact_required');

    // If value is 1, then replace our button with markup.
    if($field[0]['value'] == 1) {
        unset($form['submit']);
        $form['ask_for_product'] = array(
            '#markup' => MODULE_ask_for_product($product),
            '#weight' => 50,
        );
    }
    }

Now we need to do something with AJAX:
    function HOOK_commerce_cart_attributes_refresh_alter(&$commands, $form, $form_state) {

    if (!empty($form_state['context'])) {
        // Getting value of field_is_contact_required of new chosen product
        $product = $form_state['default_product'];
        $field = field_get_items('commerce_product', $product, 'field_is_contact_required');

        // If field_is_contact_required is set to 1, then replace a container that have class ".add-to-cart-button" (we just added that class), with our markup
        if($field[0]['value'] == 1) {
            $replacement = '.add-to-cart-button';
            $commands[] = ajax_command_replace($replacement, MODULE_ask_for_product($product));
        }

    }

    }

And our helpers:
// Return markup of our new button. 'node/7' is node of our contact form, 'subject' is a key of our query string
function MODULE_ask_for_product($product) {
    return '
' . l(t('Ask about product.'), drupal_get_path_alias('node/7'), array('query' => array('subject' => 'Ask about product with SKU: ' . $product->sku))) . '
';
}

// Prevent some products from be added to cart.
function MODULE_contact_required_validate($form, &$form_state) {

    $product = $form_state['default_product'];
    $field = field_get_items('commerce_product', $product, 'field_is_contact_required');

    if($field[0]['value'] == 1) {
        form_set_error('', t('Don\'t try to be annoying! :P'));
    }

}

That's all, you can customize it, or by extending the idea, do much more with it (maybe by using webform api).
Answer: One approach would be to add a payment plugin that would check for product property and then disallow payment until client asks for contact, and site operator marks all products as "contacted". That would allow client to keep products in a shopping cart and only ask when he feels pretty much decided. It would also allow to ask about all products that are supposed to go in one shipment, and that seems to be a good thing. Maybe there is an option to write commerce plugin that works at cart level, too, but I never tried it.
Way faster, Q'n'D approach would be to test for a given property at tpl.php level and substitute buttons. Of course it has some drawbacks, smart people still could get around it and try to force-feed their choice to basket and checkout.


If $A$ is convex then $rA+sA = (r+s)A$

Question: I am trying to show that for a convex set $A$ and $s,r>0$ positive real numbers we have $rA+sA = (r+s)A$.
Clearly $(r+s)A$ is contained in $rA+sA$ but I am having trouble showing the other inclusion.
Answer: Here's one trick—If you divide each of $r$ and $s$ by $r+s$, you get a number between 0 and 1, which allows you to make a convex sum:

Suppose $z \in rA + sA$. Then $z = rx + sy$ for some $x,y \in A$.
Because $r$ and $s$ are positive, the numbers $t_1 \equiv \frac{r}{r+s}$ and $t_2 \equiv \frac{s}{r+s}$ are between 0 and 1.
Because $A$ is convex and $x,y\in A$, we know that $t_1 x + t_2 y \in A$.
But $z = (r+s)(t_1x + t_2 y)$, hence $z \in (r+s)A$.
Answer: To show the other inclusion $ rA+sA \subset (r+s)A $:
let $\zeta \in rA+sA $, then there exist $ x,y \in A$, such that $ \zeta=rx+sy$.
Now we set $\eta =\frac{r}{r+s}x+\frac{s}{r+s}y \in A $(convexity), obviously,
$$ \zeta =(r+s)\eta \in (r+s)A $$
that is,
$$ rA+sA \subset (r+s)A $$
Answer: let $a \in rA + sA$
then $\exists b \in A, c \in A, a=rb+sc$
$$a= (r+s) \left(\frac{r}{r+s}b + \frac{s}{r+s}c \right)$$
Note that we have $\left(\frac{r}{r+s}b + \frac{s}{r+s}c \right) \in A$

If $A$ is convex then $rA+sA = (r+s)A$


PCA to reconstruct Binary Data

Question: I'm working with binary 3D matrices. I calculate their PCA (or REOF or SVD) and as a test I would like to reconstruct these matrices from the PCA results. However I realized that because I only keep the first 10 modes of my PCA, the reconstruction ends up giving me floats between 0 and 1 rather than binary numbers.
Can a PCA reconstruction (see this question) with only the first principal components return binary numbers ? (binary in -> binary out)
Comment: Welcome to CV.  Could you please elaborate on what you mean by "fit"?  What is your objective?
Comment: What, exactly, would you mean by a "PCA reconstruction"?  When you don't use all the PCs, all you get is an *approximation* to the original data in a least squares sense.  When you use all the PCs you will indeed recover the original data (up to some accumulated floating point precision which, except in cases with extremely large dimensions, you can safely just round away).
Comment: Sorry, I will modify the main text. By "fit" I mean that I am expecting a PCA reconstruction to give me back the same type of input I gave in, in that case, binary numbers. However I end up with decimals and not binary. So I was wondering if a PCA reconstruction could work for a binary dataset.
Comment: You're right maybe I should use the term *approximation*. I only use the first PCs because they explain more than 95% or so of the variance.
I guess that explains that I am not able to recover 0s and 1s if I don't use all the PCs ?
Comment: For example: I use the 5 first PCs that explain 95% of the data's variance. Wouldn't a rounding of the output give the original dataset ? (everything below 0.5 would become 0, everything above 0.5 would become 1).

Or is 5% of unrepresented variance enough to swing the value of some cells ? (for example, 0.85 would become 1 with 5 PCs, but if I reconstruct the data using all the PCs  could it actually be a 0 ?)
Answer: As you mention, you can always round the output of your floating point PCA/SVD to 0/1, and that may work well enough for your purpose. But, there are other options which are tuned to the binary case and may be able to do more with fewer components (if your data has rank higher than the rank you truncate the principal subspace to).
First, since this is stats overflow, there are probabilistic options. Standard principal components analysis is derived under the assumption that the data has a multivariate normal distribution. In other words, the answer to "which low-dimensional subspace contains the most variance?" has a nice answer in the Gaussian case. Still, for non-Gaussian data (like binary data, typically modeled with the Bernoulli distribution), similar questions can be asked and answered, and these may be more appropriate for your context. For instance, you could consider looking into logistic PCA -- its relation to PCA is something like the relationship between logistic regression and the usual (Gaussian) linear regression. A. Landgraf's R package of this name may be of interest, and that website points to some nice related literature. As in logistic regression, there are lots of related models which could be of interest. For instance, although Poisson NMF models nonnegative integer data, it is also sometimes used on binary count data (although its reconstructions may not be binary).
And second, there is lots of literature about low-rank Boolean matrix factorizations -- this webpage has many references and a tutorial. Googling this term and your programming language of choice should turn up useful implementations of standard algorithms. For instance, the Nimfa package in Python includes some.
Comment: Thank you for your answer !! I really appreciate the links, and for the Logistic PCA suggestion. I did not know about this technique, it might be more adapted to my study case.
Answer: The reconstructed dataset using a subset of principal components is an orthogonal projection: a mathematical generalization of the process of rendering a 3D scene as an image.

This famous Escher print projects a regular array of shapes into fewer dimensions.  The locations of the shapes in the paper is no longer regular (it is not a lattice).
Binary data in $n$ dimensions, whose values are limited to $0$ and $1,$ are necessarily located on the integer lattice (of vectors with integral coordinates).  But when projected, even into a space of just one dimension less (as in the Escher print), they will not necessarily appear lattice-like.  Here, for instance, is the projection of 16 binary vectors in $32$ dimensions onto the space spanned by their first two principal components:

(There is some overlap in the projections, evidenced by the stronger coloration in the overlapping points, yielding only 8 distinct points.)
It therefore is unrealistic to hope either that such a "partial reconstruction" will have all binary coordinates (which would cause all the points to fall on the integer lattice) or even that rounded versions would correctly reproduce the original data.
To demonstrate this, here are the original dataset, its reconstruction based on the top five principal components, and the rounded version of that reconstruction.  All are plotted as images in which each column represents a vector.

(This PCA centered all the columns but did not rescale them.)  This reconstruction accounts for 87% of the variance.  All the variance is accounted for by the top seven PCs, indicating this reconstruction is a projection from a space that is inherently of seven dimensions into a space of five dimensions.
You can see that many of the reconstructed coordinates are close to $0$ (lightest color) or $1$ (darkest color), but not all are.  At the right, you will notice the rounded reconstruction looks substantially like the original, but not quite.  About 1.2% of the coordinates flipped from $0$ to $1$ and another 2.0% of them flipped from $1$ to $0.$

Here, to show the details of the calculations, is the R code used to generate this example.
#
# Generate binary data.
#
set.seed(17)
p <- 16   # Number of observations
n <- 32   # Dimension of the vectors (cannot be less than `p`)
kmax <- 3 # Roughly, determines the rank of the matrix
A <- matrix(sample(0:1, p * kmax, replace = TRUE), ncol = p)
X <- (matrix(sample(0:1, n * kmax, replace = TRUE), n) %*% A) %% 2
#
# Perform PCA.
#
obj <- princomp(X)
screeplot(obj)      # Charts contributions from the highest PCs
with(obj, signif(cumsum(sdev^2)[1:10] / sum(sdev^2), 3)) # Their contributions
#
# Reconstruct the data from a specified number of PCs.
#
k <- 5 # PCs used for reconstruction
X. <- with(obj,
           t(zapsmall(loadings[, 1:k, drop = FALSE] %*% t(scores)[1:k, , drop = FALSE]
                      + center)))
#
# Display `X.`, `X`, and its rounded version as images.
#
par(mfrow = c(1,3))
i <- list(Reconstructed = X., Original = X, Rounded = round(X.))
for (s in names(i)) {
  image(i[[s]], bty = "n", xaxt = "n", yaxt = "n", main = s)
}
par(mfrow = c(1,1))
#
# Evaluate the differences between the original data and the rounded reconstruction.
#
table(X - round(X.)) / length(X)
with(obj, plot(loadings[, 1:2], pch = 21, bg = hsv(0.02, 1, 1, 0.3),
               xlab = expression(PC[1]), ylab = expression(PC[2])))

Comment: Thank you very much for this great example !
I will have to think of a way to maximize the variance reconstructed without taking too many Principal components, but I will also take a look at how many pixels flip when rounded. If it is about 1% it should be good enough for my case study.

Thank you for the code as well, it is very instructive !

PCA to reconstruct Binary Data


Function notation when $f^n=g^n$ at some $n$?

Question: What is the proper terminology / symbol to use for a function that reaches some point/value or a variable? Not necessarily converge to it, but passes it somehow?
Example
For $x\in\mathbb{N}$. Set initially $x_0=2$.
Iterating the function $f(x) = 2x$ will at some point reach the value $64$ for example: $\{4,8,16,32,\underline{64},128,256,..\}$. For this case, $f(x)$ passes $x_0^6$ ? (i.e. $x_0$ raised to $6$).
Or do you say, for some $n$ the function $f^n(x)$ will reach or pass the value $64$ or $x_0^6$?
If we use two functions, is it possible to say the function $f^n$ passes through function $g^n$ at some $n$? or $f^n = g^n$ at some $n$ ?
Answer: To answer the question in the body, we say that "$64$ is in the orbit of $2$ under $f$".
More generally, if $f^n(x_0)=y$, then say that "$y$ is in the orbit of $x_0$ under $f$".


reading first line of a file in c

Question: I am very new to C and I am having trouble with the most fundamental ideas in C.  We are starting structures and basically the assignment we are working on is to read a delimited file and save the contents into a structure.  The first line of the file has the number of entries and alls that I am trying to do at the moment is get the program to read and save that number and print it out.  Please do not assume I know anything about C I really am very new to this.
This code is giving me a segmentation fault
<code>#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct info{
  char name[100];
  char number[12];
  char address[100];
  char city[20];
  char state[2];
  int zip;
};

int strucCount;
char fileText[1];

int main(char *file)
{
  FILE *fileStream = fopen(file, "r");
  fgets(fileText, 1, fileStream);

  printf("\n%s\n",fileText);

  fclose(fileStream);

}
</code>
Here is the sample file
<code>4
mike|203-376-5555|7 Melba Ave|Milford|CT|06461
jake|203-555-5555|8 Melba Ave|Hartford|CT|65484
snake|203-555-5555|9 Melba Ave|Stamford|CT|06465
liquid|203-777-5555|2 Melba Ave|Barftown|CT|32154
</code>
Thanks for everyones comments, they helped a lot, sorry to Jim.  I am working on very little sleep and didn't mean to offend anyone, I am sure we have all been there haha.
Comment: @paulsm4 No, the OP's segmentation fault is in fgets, because of an attempt to open a non-existent file.
Comment: try something like this http://cfiddle.net/HPitJP
Comment: I meant I know what I have learned in class thus far, sorry for the confusion.
Comment: Hi - Sidharth Mudgal answered your immediate question: you need to make your read buffer ("fileText") bigger; this should fix the segmentation violation. ALSO: the parameters to `main(int argc, char *argv[])` are wrong. Q: Could you post an example line from your text file?
Answer: SUGGESTION:
<code>    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    #define MAXLINE 80
    #define MAXRECORDS 10

    struct info{
      char name[100];
      char number[12];
      char address[100];
      char city[20];
      char state[2];
      int zip;
    };

    int
    main(int argc, char *argv[])
    {
      FILE *fp = NULL;
      int nrecs = 0;
      char line[MAXLINE];
      struct info input_records[MAXRECORDS];

      /* Check for cmd arguments */
      if (argc != 2) {
        printf ("ERROR: you must specify file name!\n");
        return 1;

      /* Open file */
      fp = fopen(argv[1], "r");
      if (!fp) {
        perror ("File open error!\n");
        return 1;
      }

      /* Read file and parse text into your data records */
      while (!feof (fp)) {
        if (fgets(line, sizeof (line), fp) {
          printf("next line= %s\n", line);
          parse(line, input_records[nrecs]);
          nrecs++;
        }
      }

      /* Done */
      fclose (fp);
      return 0;
    }
  fclose(fileStream);

}
</code>
Key points:

Note use of "argc/argv[]" to read input filename from command line
line, nrecs, etc are all local variables (not globals)
Check for error conditions like "filename not given" or "unable to open file"
Read your data in a loop, until end of input file
Parse the data you've read from the text file into an array of binary records (TBD)
Comment: @Zach It's a typo ... it should be `struct info input_record[MAXRECORDS]`. `struct info` is a type, so it's just defining a variable called input_record that is an array of MAXRECORDS struct infos.
Comment: Well I thank you for your help thus far, sorry to be offensive.
Comment: Thank you for the suggestion, there is a lot going on in this that I cannot follow, it kills me I can write a complex java program but C is killing me.  What is the function of the int argc, all that I want to pass to the program is the file name, such as ./prog info.txt what function does the other parameter serve?
Comment: Ah I got it to read the first line, thank you for this suggestion even though I am not sure what it all means at the moment
Comment: Q: What is the function of `main (int argc, char *argv[])`?  Like Java, "main()" has a standard format.  You can't mess with it ;).  "argc" is the #/command line arguments (including program name); argv is the arguments themselves (just like "args[]" in Java, except "argc[0]" is the executable's name and path).  "argc" is equivalent to Java "args.length".
Comment: The struct input_record is that just making an array of size 10?
Comment: @Zach Ok, to be honest I have absolutely no interest in helping someone who wants other people to do his work for him. Goodbye.
Comment: @Zach Of course it's not what you meant, it's what you're *doing*. *Sheesh* indeed.

reading first line of a file in c


MySQL php query flaws

Question: I am to do a presentation on SQL Injection, and what better way to do it than to make my own php queries and show them to the class.
The problem is I have my DB and php code running flawlessly, but can't seem to find an exploit on my UPDATE query in an edit form I made.
If anyone could take a look at it and tell me how I could insert say a 'DROP TABLE' command or something of the sort, it would be extremely helpful!
<?php
session_start();

require_once 'config.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

$userid = $_SESSION["userid"];
$fname = $_SESSION["fname"];
$lname = $_SESSION["lname"];
$gender = $_SESSION["gender"];
$email = $_SESSION["email"];

if($_SERVER["REQUEST_METHOD"] == "POST") {

    $userid = trim($_POST["userid"]);
    $fname = trim($_POST["fname"]);
    $lname = trim($_POST["lname"]);
    $gender = trim($_POST["gender"]);
    $email = trim($_POST["email"]);

    $query = "UPDATE user_details SET fname = '$fname', lname = '$lname', gender = '$gender', email = '$email' WHERE userid = '$userid' ";

    if (mysqli_query($link, $query)) {

        echo 'Update complete!';

    }
    echo mysqli_error($link);
    // else {

    //     echo '
' . 'Woah something went really wrong dude' . '
' ;
    //     echo mysqli_error($link);
    // }

}

?>

<!DOCTYPE html>

    <meta charset="UTF-8">
    <title>Edit</title>
    <link rel="stylesheet" href="bootstrap.css">

        <form class= "form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            First Name
            <input type="text" name="fname" class="form-control" value="<?php echo $_SESSION["fname"]; ?>">
            Last Name
            <input type="text" name="lname" class="form-control" value="<?php echo $_SESSION["lname"]; ?>">
            Gender
            <input type="text" name="gender" class="form-control" value="<?php echo $_SESSION["gender"]; ?>">
            Email
            <input type="text" name="email" class="form-control" value="<?php echo $_SESSION["email"]; ?>">

            UserID
            <input type="text" name="userid" class="form-control" value="<?php echo $_SESSION["userid"]; ?>">
            <button type="submit">Submit</button>

    </form>
    <form action="action.php"><button type="submit">Back</button></form>

Comment: I have tried to piggy back a query by inputting as $userid 1234' ; DROP TABLE table_test --    but that didn't seem to work, currently looking more into how I could properly do it, but I'm sure that my noob code will be easy to crack for most people on this site
Comment: "I have tried to piggy back a query by inputting as $userid 1234' ; DROP TABLE table_test -- but that didn't seem to work" mysql_query doesn't support multiple SQL statements.
Comment: "The problem is I have my DB and php code running flawlessly, but can't seem to find an exploit on my UPDATE query in an edit form I made" only way you can exploit this is by using a timebased SQL injection vector.. `UPDATE user_details SET fname = (SELECT SLEEP(5)) --` for example
Answer: In the most of the cases mysqli prevents multiple queries and MySQL itself has certain in-built security measures to prevent accidental deletion of tables and databases. So your DROP TABLE won't work.
But you can demonstrate a successful login attempt using MySQL injections.
Create a login form and pass data to the validation page. There write your query as,
query =  "SELECT * FROM `user`
     WHERE `username` = '." username ".' AND `password` = '." password ".'";

Now, in the login form enter username as ' OR '1'='1 and password as ' OR '1'='1. so at the end the complete query would look like this,
SELECT * FROM `user` WHERE `username`='' OR '1'='1' AND `password`='' OR '1'='1';

this will return all the user records in the user table, and simply the application would login by using the very first record.
Comment: Thanks for the answer! You saw right through me, I already have a working registration and log in example made which showcase what you just typed, but I wanted to show a DROP TABLE example on the edit form as the last example and leave the class shocked a show my DB changed (table dropped) by injecting the query in the edit.php form.
I'll just change the function call to mySQL instead of mySQLi since it's so secure
Comment: Yep try that too. but as i've said MySQL itself has some security built in. You may not have guaranteed results of `DROP TABLE`. But this login attempt is guaranteed.
Answer: Not sure I understand you fully, but I will try to tell you why this seems to be a bad idea...
if I post to userid
$userid="';DROP TABLE XY;SELECT * FROM USER_DETAILS WHERE fname = '"

your code would do...
$query = "UPDATE user_details SET fname = '$fname', lname = '$lname', gender
= '$gender', email = '$email' WHERE userid = '$userid' ";

which results in the query:
$query = "UPDATE user_details SET fname = '$fname', lname = '$lname', gender
= '$gender', email = '$email' WHERE userid = '';DROP TABLE XY;SELECT * FROM
USER_DETAILS WHERE fname = '' ";

The queries would be
UPDATE user_details SET fname = '$fname', lname = '$lname', gender
= '$gender', email = '$email' WHERE userid = ''

DROP TABLE XY

SELECT * FROM USER_DETAILS WHERE fname = ''

Each one of them is valid...now create a table XY and run the original query of your example with this posted injection...
I am missing something?
Edit: as @RaymondNijland pointed out the mysql function used in the example seems to block this kind of an attack. I am still considering it to be a dangerous code style, where only a driver creates security. Maybe there are future updates allowing batch sql, I would sleep better knowing my code doesn't open this kind of a door...
Comment: mysql_query doesn't support multiple SQL statements so it will not work..
Comment: No that's perfect my man Peter thanks I will try it.
Edit: The idea is to find vulnerabilities and EXPLOIT them, since this is supposed to be an example to the class of how not to do things and the consequences of having a vulnerable system
Comment: Raymond, do we still agree this is a bad idea?
Comment: @RaymondNijland I see, so should I use a different function for the query? Again the idea is for the system to be vulnerable to SQL Injection to be shown as an example to the class
Comment: @john, if it helps you please mark the answer "correct"
Comment: @John only way you can exploit your UPDATE query is by using a timebased SQL injection vector.. `UPDATE user_details SET fname = (SELECT SLEEP(5)) --` as simple example the multiple SQL statements won't work.
Comment: @RaymondNijland Alright so what could I do to make it vulnerable to a DROP TABLE injection? Use mySQL_query instead of mysqli?
Comment: I was updating the answer to cover the topic opened by @RaymondNijland...
Comment: @John mysql_query or mysqli_query arn't vulnerable to multiple SQL statements injections and it's not possible  too make them vulnerable.. If the programmer was using mysqli_multi_query this injection will work just fine .

MySQL php query flaws


How do I programmatically retrieve the inputs from a form?

Question: I am implementing a hook_form_alter in my custom module and I want to get all the inputs from the $form variable.
So, if for example there is a textarea, a radio button and a checkbox, I want to grab these elements and put them in a clean array for later use. What I basically want to do down the road is create a dropdown list and have these inputs as the options in the list.
Is this a clean way to do that through the FAPI?
Answer: You can use the element_children() function to grab all of the child elements within a particular element (including a form element). The thing with Drupal forms is that often all elements are not on the same level of the array, so you'll need to make a recursive function to get them all reliably. This is untested but should give you a place to start:
function _element_children_recursive(&$element) {
  $names = array();

  foreach (element_children($element) as $element_key) {
    $names[] = $element_key;
    $names = array_merge($names, _element_children_recursive($element[$element_key]));
  }

  return $names;
}

$all_element_names = _element_children_recursive($form);

There's even one further caveat to this...since form elements can be on different levels you might well end up with two identically named keys. This is perfectly valid in Drupal so you'll probably want to do a bit of checking in the recursive function to avoid duplicates.
Comment: I used the idea in your answer, but my code looks like this: http://pastebin.com/csJstrwT (added some restrictions to keep only the elements that are visible to, and accessible by, the user)
Answer: Do you want the FAPI representation of the elements or the user-inputted values?
I think that you are asking about the values input to the form elements by the user. These are available in the variable $form_state['values'] when the form has been submitted.
Comment: No, I'm actually asking about the FAPI representation of the elements. Ultimately I want to grab all the form elements that are visible to the user when he sees the form.
Answer: I think the accepted answer is the best, but I wanted to mention you can also get the field names by doing drupal_retrieve_form('form_id', $form_state). You get an array with the form element names as keys, and the build attribute array of each field as the contents.
Note: you have to create a dummy $form_state by doing $form_state['build_info']['args'] = array();


How can I programmatically alter Webform "To Email Address"?

Question: I want to change email addresses based on postal codes entered by users.
My code looks like below.
function my_webform_tracker_webform_submission_render_alter(&$renderable) {
  $node_id = (int)$renderable['#node']->nid;
  if ($node_id == 257) {
    $postal_code = (int)$renderable['#submission']->data[5][0];
    if($postal_code >= 14000 && $postal_code <= 14990) {
      $renderable['#email'][email] = "firstname.lastname@example.com";
      $renderable['#node']->webform['emails'][1]['email'] = "email@example.com";
      $renderable['#node']->webform['emails'][2]['email'] = "firstname.lastname@example.com";
      //print "
"; print_r($renderable); exit;
    }
  }
}

I tried a lot but the email id seems to be not changed. Please help me figure this out.
Answer: I think a more apt place to alter the destination email would be hook_mail_alter() where Drupal lets you alter an email before sending it out. The webform module includes all the submission data in the $message['params']['submission'] object.
You can do something like this in your module:
/**
 * Implements hook_mail_alter().
 */
function MYMODULE_mail_alter(&$message) {

  //Catch Webform submissions when we have a valid postal code
  if($message['id'] == 'webform_submission') {
    $postal_code_component = 5;
    if(isset($message['params']['submission']->data[$postal_code_component][0])) {
      $postal_code = (int) $message['params']['submission']->data[$postal_code_component][0];

      //Find right addressee to switch to
      if(0 <= $postal_code && $postal_code <= 13999 ) {
        $message['to'] = 'firstname.lastname@example.com';
      } elseif(14000 <= $postal_code && $postal_code <= 14990) { //Etc...
        $message['to'] = 'firstname.lastname@example.com';
      } else { //Catchall
        $message['to'] = 'email@example.com';
      }
    }
  }
}

Answer: I figured it out, actually I was using wrong hook to do the task. The right on would be "hook_webform_submission_presave". So my code will look like,
    function my_webform_tracker_webform_submission_presave($node, &$submission) {
      $postal_code = (int)$renderable['#submission']->data[5][0];
      if($postal_code >= 14000 && $postal_code <= 14990) {
        $node->webform['emails'][1]['email'] = "email@example.com";
        $node->webform['emails'][2]['email'] = "firstname.lastname@example.com";
      }
    }

Thank You everyone for help!!


Is there a way to show the content right-to-left without manually changing CSS styles?

Question: In a site where some of the pages are written using the Hebrew alphabet, I created a view that displays linked headings of nodes. The title is shown on the right, but the linked headings are on the left.
Is there a way to show a view right-to-left without manually changing CSS styles?
Answer: An alternative way so you don't have to change css all the time.
You can probably setup a generic RTL css rule in your theme (one time)
.rtl {
    direction: rtl;
}

and use this rule in each of your views under advanced > css class as you see fit.
Alternatively as Leymannx points out you can add global text area in the header of your view and add the following css

using Full HTML format as shown in the screenshots below

NOTE: view class is included in all views so this solution could potential affect all views on the page


Montar Playlist Dinâmica Com Select Option

Question: Pretendo criar várias "Play List" utilizando um elemento select, ao invés de deixar no corpo do documento HTML, quero colocar em um arquivo "js" externo, e chamar cada "Play List" através de um "Campo de busca" onde deve-se digitar a Categoria correspondente, então será exibida dentro de um elemento form cujo o "id" está definido como "playlist".
Comment: da uma olhada nisso aki http://stackoverflow.com/questions/1502649/jquery-getjson-populate-select-menu-question ou nisso http://stackoverflow.com/questions/5918144/how-can-i-use-json-data-to-populate-the-options-of-a-select-box
Answer: Consegui fazer desta forma ..

    <form name = "playlist">

    <select name = "selecao" multiple size = "10"> </select>

    </form>

 /* Acrescente nestas variáveis os Nomes de cada Categoria */
 /* E insira seus respectivos resultados dentro de Cada Array */
 humor = new Array("A", "B", "C");

 guerra = new Array("1", "2", "3");

 terror = new Array("I", "II", "III");

 /* Esta rotina destina-se em fazer a mudança entre as PlayList */
 /* na qual foi definida Acima, exibindo todo resultado no Select */

 function TrocarLista(formulario, listagem, genero) {

     if (genero == "humor") {

         var lista = humor;

     } else {

         if (genero == "guerra") {

             var lista = guerra;

         } else

             var lista = terror;

     }

     /* Nesta parte é feito uma varredura de cada elemento */
     /* que contém no PlayList, nos dando os números exato. */

     var numero = document.forms[formulario][listagem];

     numero.options.length = 0;

     for (i = 0; i < lista.length; i++) {

         var item = lista[i];

         numero.options[i] = new Option(item);

     }

 }

 /* Agora para finalizarmos, criei o campo de busca simples */
 /* para efetuar a Busca por Categoria/Gênero se tratando de */
    /* uma PlayList de - Filmes, Vídeos, Clipes, Áudio, Músicas */

 buscar = function() {

     var mostra = document.getElementById('categoria').value;

     switch (mostra) {

         case 'guerra':
             TrocarLista('playlist', 'selecao', 'guerra');
             break;

         case 'humor':
             TrocarLista('playlist', 'selecao', 'humor');
             break;

         case 'terror':
             TrocarLista('playlist', 'selecao', 'terror');
             break;

         default:
             alert('Desculpe, nada encontrado.');

     }

 }

Para fazer Teste e ver o resultado, basta digitar no Campo de busca uma das Categoria pré-definida: humor, guerra ou terror

Não acrescentei muitos comentários para deixar o código limpo e espaçado, mas ... me preocupei em fazê-lo auto-descritivo.

Espero que esta Questão e sua Resposta ajude outros colegas.


Show that a facet of a polytope lies outside unit ball

Question: Let $P$ be a convex polytope in $\Bbb R^n$ with the origin in its interior such that for all vertices $x$ of $P$ we have $||x||_2 \geq 3$. Then at least one facet of $P$ does not intersect the Euclidean unit ball $B_2^n=\{x\in\Bbb R^n: ||x||_2\leq 1\}$.
Do you think that this is true? If so, can anyone provide a strategy to prove this? If, in order to make the conclusion hold, one needs to assume that all vertices satisfy $||x||_2 \geq c$, where $c>3$ is a universal constant (independent of the dimension $n$ and the polytope $P$), that is fine.
Thank you in advance.
Answer: I think this is false: consider, for large $n$, the hypercube with vertices
$(3/\sqrt{n})(\pm1,\pm1,\ldots,\pm1)$.


Alternar entre modais

Question: Quero fazer um modal onde posso visualizar o cadastro e se eu clicar para Editar subir o outro modal, assim alternando entre os formulários.
HTML com modal:
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

Modal title

        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          ×
        </button>

        <form>
           Exemplo
           <button type="button" class="btn-editar">Editar</button>
        </form>

         <form>Exemplo2
          <button type="button" class="btn-canc-edit" >Cancelar Edição</button>
         </form>

        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>

Javascript:

CSS:

Quando eu clico para aparecer o de Editar, ele aparece, mas não fica em cima do de visualizar.
Answer: você pode fazer o seguinte, substituir a função toogle() por atacar direto o css. Sendo assim você altera o atributo "display" da classe CSS "visualizar" e editar. mostrando e ocultando os formulários.
Editei seu código java script, veja se isso ajuda.

Answer: Tem uma técnica fácil para isso:
Apague o seu script e CSS e tente com este código:

Além disso, você pode criar outro modal, por exemplo, deixa este como #modal_visualizar e cria um como #modal_editar.
Desta maneira, quando o usuário clicar no botão .btn-editar que está no #modal_visualizar, o mesmo irá se esconder e irá exibir o #modal_editar. Dentro do #modal_editar, você pode criar um botão para retornar ao modal de visualização.
Então ficaria assim:

Bom, eu particularmente faria a segunda opção, pois terá um efeito muito mais agradável para o usuário final.
Espero ter te ajudado :)

Alternar entre modais


Propriedade contendo apenas o atributo KeyAttribute resultando em coluna auto-incremento no banco de dados

Question: Se não me engano, isso deve ter vindo na versão 6.1.1 do EntityFramework e não era assim nas versões anteriores.
Acredito que uma coluna só se tornava autoincremento quando o atributo DatabaseGenerated era declarado com a assinatura [DatabaseGenerated(DatabaseGeneratedOption.Identity)].
Tenho uma classes simples, assim:
public class Municipio
{
    [Key]
    public int Id { get; set; }

    [StringLength(50)]
    [Required(AllowEmptyStrings = false)]
    public string Descricao { get; set; }

    [StringLength(2)]
    [Required(AllowEmptyStrings = false)]
    public string Uf { get; set; }
}

Veja que a propriedade Id está declarada com o atributo KeyAttribute apenas. Porém, está sendo gerada uma coluna Autoincrement.

Como impedir que isso aconteça?
Answer: Acredito que uma coluna só se tornava autoincremento quando o atributo DatabaseGenerated era declarado com a assinatura [DatabaseGenerated(DatabaseGeneratedOption.Identity)].
Não necessariamente. Desde o Entity Framework 5 o uso do atributo é opcional.
Como impedir que isso aconteça?
Usando [DatabaseGenerated(DatabaseGeneratedOption.None)].


Empty result list on django-filter page startup

Question: I'm using https://github.com/alex/django-filter
When user opens the page with filter at the first time, they see the empty form and full list of results.
I need to not to display the result until user press Search for a first time.
In other words - django-filter should not display any results if parameters list (in url) is empty, and display all results if parameter values (in url) is empty.

Source
filters.py
import django_filters

from product.models import Product

class ProductFilter(django_filters.FilterSet):

    class Meta:
        model = Product
        fields = [ 'shortname', 'fullname', 'description', 'product_type' ]

views.py
from product.filters import ProductFilter

def product_search_in_ancestors(request, product_id):
    context = RequestContext(request)
    p = get_object_or_404( Product, pk=product_id )

    q = Product.objects.filter( id__in = p.GetChilds() )

    filter = ProductFilter(request.GET , queryset=q )

    return render_to_response( 'product/product_search_in_ancestors.html',
        {'product':p,
        'filter': filter
         },
         context)

Link to same issue on github
Comment: The problem is, the default view of a Filter is to return ALL the results. You may need to edit your view. Can you post it?
Comment: I've post my very simple code
Comment: I was going to say that you could prevent the display of results if there were no parameters in the url, but I realized this would prevent the display of the form as well. So I'm not sure if what you want is possible unless you create a seperate page for just the form, and another with the form/results.
Comment: In best - I wish to display form and results on single page. This is default behavior of django-filter.
Answer: After some studies, I've found this simple solution
class ProductFilter(django_filters.FilterSet):
    class Meta:
        model = Product
        fields = [ 'shortname', 'fullname', 'description', 'product_type',   ]

    def __init__(self, *args, **kwargs):
        super(ProductFilter, self).__init__(*args, **kwargs)
        # at sturtup user doen't push Submit button, and QueryDict (in data) is empty
        if self.data == {}:
            self.queryset = self.queryset.none()

I think, this solution is stable. How do you think?
Comment: When I try your solution my filter form does not render.
Comment: Yes, when I place this in my init the records do not show up, but neither does the filter.form containing my filters. If I manually add parameters to the get querystring the form renders but as default it is just not rendering in the template. I am using crispy forms, if that makes a difference.
Comment: May be my solution does not aplly to crispy, I do not test them
Comment: Post some more details, please


Marshalling in c# ( Passing structure to an unmanaged type )

Question: I have a c++ dll which exposes the following function
long func(struct name * myname)
{
     strcpy(myname->firstname,"rakesh");
     strcpy(myname->lastname,"agarwal");
     return S_OK;
}

struct name
{
    char firstname[100];
    char lastname[100];
}

I want to call this function from a C# application , so I do the following :
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
unsafe public struct name
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=100)]
    public string firstname;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
    public string lastname;
} ;

[DllImport("C++Dll.dll")]
public unsafe static extern long func(name[] myname);

name[] myname = new name[1];
func(myname);

The application builds successfully. When the C# application .exe is run, the function func() is called successfully and it is able to populate the fields successfully inside the dll. But when the function returns to the C# application, the variable myname still conatins null values for the struct fields(firstname and lastname).
Please suggest changes so that I am able to populate the fields values of myname (so that after the function func() finishes execution, the variable myname->firstname contains "rakesh" and myname->lastname contains "agarwal".
Note: StringBuilder cannot be used inside the structure.
Answer: Instead of using an array, pass the struct by reference.  For a PInvoke call, a ref struct will be translated into a pointer to the struct.  The ref argument also tells the CLR to marshal data in both directions, to native code and then back out again.
[DllImport("C++Dll.dll")]
public unsafe static extern long func(ref name myname);

Also, if you're doing a lot of interop work I suggest you check out the PInvoke interop assistant (link).  This tool will automatically convert the majority of C type definitions (including structs, enums, unions and function pointers) into their corresponding C# or VB.Net types.  It will additionally convert signatures which contain these types.
Answer: Declare the import as public unsafe static extern long func(ref name myname), and call it as:
name myname = new name();
func(ref myname);

You probably don't need the unsafe declarations on the name structure or import declaration, by the way.


Getting the "Solarium @version is too old" error after upgrade

Question: I recently updated a Drupal 9.3 site to Drupal 9.5 and most of the contributed modules were updated to the latest version that supports both 9 and 10 in view of an upcoming Drupal 10 upgrade.
On the status report page, I am getting this error.

Solarium @version is too old!

How do we rectify this error? The Solr search module is locking Solarium to an old version and we might need to force an upgrade on this. Is there a better way to do this?
More details of the composer.lock file with solarium set to 6.3.3
<code>{
        "name": "drupal/search_api_solr",
        "version": "4.3.1",
        "source": {
            "type": "git",
            "url": "https://git.drupalcode.org/project/search_api_solr.git",
            "reference": "4.3.1"
        },
        "dist": {
            "type": "zip",
            "url": "https://ftp.drupal.org/files/projects/search_api_solr-4.3.1.zip",
            "reference": "4.3.1",
            "shasum": "002061b1160df5da5d13eea6f3a4c50f8008b157"
        },
        "require": {
            "composer-runtime-api": ">=2.0",
            "composer/semver": "^1.0|^3.0",
            "consolidation/annotated-command": "^2.12|^4.1",
            "drupal/core": "^9.3 || ^10.0",
            "drupal/search_api": "~1.30",
            "ext-dom": "*",
            "ext-json": "*",
            "ext-simplexml": "*",
            "laminas/laminas-stdlib": "^3.2",
            "maennchen/zipstream-php": "^2.2.1|^3.0.2",
            "solarium/solarium": "^6.3.3"
        },
</code>
Comment: You have an outdated version of search_api_solr. The current version (4.3.2) uses Solarium 6.3.5, which is the latest
Comment: The entire message template is: `Solarium @version is too old! Version required: @required`. Please add to the Question the entire error. In addition I think we would need to see the composer.json file to actually form an answer. We don't know what versions you have at this time or what you mean by "Solr search module is locking". Are you typing Composer commands that are not having the desired effect? If so, which commands (precisely), and what is their output?
Comment: Added the versions in the lock file to the question description
Answer: I had the same problem on a Drupal 9 with version 4.3.1 of search_api_solr, because version 4.3.2 is no longer compatible with D9 but only: ^10.1 || ^11.0
So I've been using version 4.2.12 while waiting to upgrade to D10.
<code>composer require 'drupal/search_api_solr:4.2.12' -W
</code>
Do not forget the "-W" for get the Downgrading solarium/solarium works (6.x-dev b785b8b => 6.3.1)
And I no longer get the error in the dashboard


Simple (?) inequality with complex numbers and absolute value

Question: I am trying to prove the following:
$$ \left| \frac{z^{2n}}{2+z^n+ z^{5n} } \right| \leq \frac{|z|^{2n}}{2\left| 1 - |z| \right| } $$
whenever $|z|<1$. Moreover, I am invited to use the following:
$$ ||z|-|w|| \leq |z-w| \leq |z|+|w| $$
Comment: If $|z|<1$ then $|z|^{5n} \le |z|^n <1$. $|2+z^n+z^{5n}| \ge 2-|z|^n -|z|^{5n} \ge 2-2|z|$.
Answer: Whenever $|z|<1$, we have that higher powers of $z$ are smaller, i.e., $|z^m| < |z|$ for $m>1$. Thus
\begin{align*}
\Big|\frac{z^{2n}}{2+z^n+z^{5n}}\Big| &= \frac{|z|^{2n}}{|2+z^n+z^{5n}|}\\[5pt]
 &\leqslant \frac{|z|^{2n}}{||2|-|z^n+z^{5n}||}\\[5pt]
 &\leqslant \frac{|z|^{2n}}{|2-|z+z||}\\[5pt]
 &= \frac{|z|^{2n}}{2|1-|z||},
\end{align*}
as required.


Time Series Hold Out Data not used to build model

Question: It is my understanding that if one wants to build multiple time series models on a time series that goes from 2000 to today (2015) monthly; and one wanted to use that information to forecast 3 months in the future, it is common approach to split your data into "train" and "test" datasets.
Your test dataset would be the last 3 months of your time series (jan 2015, feb 2015, march 2015) (pretend we're already in april for simplicity sake). You would then 'define' your model on your training dataset, and then compute it's errors against your test dataset (defined as predicted vs actual).
This way you could try out many multiple models and pick the one with the lowest "forecast prediction error".
However my question is: By ignorning those last 3 months of data, how do you then use that model to forecast values later in time?  Example: say you wanted to forecast April-June. Is it standard procedure to apply the same model (that wasn't built on the last 3 months) to the April - June forecast period?
If so is this something you can do in R with a package? It seems like the forecast function only works to forecast forward from the training dataset, and you can't apply it to other time series objects.
Or does one 're-build' the model on the entire time series (2000 to 2015 March) and then use that model to forecast into April-June?
I am pretty confused by this and any help would be appreciated.
Answer: Well, technically you should have your model ex ante determined, and the so-called train data set is really just to demonstrate the universal validity of your model on "the present day" (test) data set. It is a subsample test.
Once the model you're using is shown to work robustly as a predictor in all important subsamples, and you've argued convincingly that it is universally valid, it's typical to run the whole thing again using all the data and try to predict the future. This part usually tends to be the problematic part.
Answer: Here's an attempt to explain this with an example.
Say we want a function or black box
$\qquad$ In: 12 months data, say rainfall
$\qquad$ Out: a forecast of the next 3 months' rainfall
$\qquad$ E.g. Jan .. Dec --> next Jan
$\qquad \quad \ \ $ Feb .. Jan --> next Feb ...
Assume further that some magic process, trained on the data from 2000 .. 2014,
tells us that a good predictor of 12 months --> the next 3 months is exactly
$\qquad$ 50 % year ago $\ + \ $ 50 % month ago.
E.g.
(Jan 2000 + Dec 2000) / 2  ~=  Jan 2001
...
(Dec 2013 + Nov 2014) / 2  ~=  Dec 2014

(Exercise: plot these 50 - 50 estimates against the real values in 2000 .. 2014, for your data.)
Now say the numbers for 2014 happen to be exactly 1 2 3 .. 12.
Then our 50 - 50 predictor gives
Jan 2014 .. Dec 2014  1 2 3 .. 12           -->  Jan 2015 forecast (1 + 12) / 2 = 6.5
Feb 2014 .. Jan 2015    2 3 .. 12 6.5       -->  Feb 2015 forecast (2 + 6.5) / 2 = 4.25
Mar 2014 .. Feb 2015      3 ..    6.5 4.25  -->  Mar 2015 forecast (3 + 4.25) / 2 = 3.625

Do you see how the Feb 2015 forecast uses the Jan 2015 forecast 6.5,
and the Mar 2015 forecast uses both the Jan 6.5 and Feb 4.25 ?
(Exercise: repeat this with numbers 1 .. 12, but 20 % year ago $\ + \ $ 80 % month ago;
repeat on your real data.)
Now we can compare the 3-month forecasts trained on 2000 .. 2014
with the real values for Jan Feb Mar 2015,
which is the reason for all this (holding out the last 3 months).
Otherwise, we'd have a black-box forecaster with magic parameters (50 - 50)
but no independent test of how it performs on new data.